单击单选按钮后更新值,并且还需要会话变量中的值

时间:2017-12-11 12:20:06

标签: javascript php jquery ajax

我有两个单选按钮。一个用于已发货,另一个用于未发货。如果我点击第一个单选按钮,则应该在总价格中添加金额100,如果我再次点击未发货,那么金额0应该添加旧的总价格。更新后的值应该是会话变量。

我的代码是:

  function LaunchURLScript() {
    $.post("/Account/Start_Appl", {}, function (data) {

        if (data = "ok") {
            alert("ok");
            var url = "myAppa:"; window.open(url); self.focus();
        }

    });

}  

我的javascript是:

<?php
 $_SESSION['fulltotalofferprice1']=$fulltotalofferprice;
?>
  <input name="shipping" type="radio" id="RadioGroup1_0" value="100" onclick="updateTotal(this.value)" />Shipped
  <input type="radio" name="shipping" value="0" id="RadioGroup1_0" checked="checked" onclick="updateTotal(this.value);" />Not Shipped

<tr><td>total Offer price: </td><td><span id="total"></span></td></tr>

我还需要在会话中更新价格。我可以将它用于更多页面。

1 个答案:

答案 0 :(得分:1)

所以你可能想要这样的东西:

首先,让我们从无线电中移除onclick id,因为id属性应始终在文档中是唯一的:

<?php
 $_SESSION['fulltotalofferprice1']=$fulltotalofferprice;
?>

<input name="shipping" type="radio" value="100"/>Shipped
<input type="radio" name="shipping" value="0" checked="checked"/>Not Shipped

<tr><td>total Offer price: </td><td><span id="total"></span></td></tr>

然后你需要设置一些脚本:

<script type="text/javascript">
   //We don't want this to change, so lets keep this as a "const"
   const originalTotal = '<?php echo $_SESSION['fulltotalofferprice1'];?>';

   //We add a listener to changes in the radios
   $( 'input[name="shipping"]' ).on('click change', function () {

       //the changed/clicked radio
       let that = $( this );

       //if the radio is checked
       if( that.is( ':checked' ) ) {

           //Add the radio value to the original value
           let total = originalTotal + that.val();

           //Set the text of the span
           $( '#total' ).text( total );

           //update the session var with a ajax post
           $.post('update-session-total-shipping.php', { total });

       }

   } );
</script>

现在您已拥有脚本,您需要创建一个php文件来接收和更新总价值,我们将其称为&#34; update-session-total-shipping.php&#34;在这个例子中:

//check if the total has a value
if( isset( $_POST[ 'total' ] ) )
{
    //set as the new total
    $_SESSION[ 'fulltotalofferprice1' ] = $_POST[ 'total' ];
}

其余的由您决定,祝您好运,永不停止学习。