简体   繁体   English

当购物车中的数量发生变化时,如何使用PHP中的jquery更新价格

[英]How to update price when quantity changes in a shopping cart using jquery in PHP

Hi I am developing a shopping cart using jquery.So far I have listed the products and their details. 嗨,我正在使用jquery开发购物车。到目前为止,我已经列出了产品及其详细信息。 Here is my code 这是我的代码

foreach( $_SESSION['r'] as $key=>$ar):

$cnt++;
 ?>   <tr>

        <td align="center"><?php echo $ar['Name']?></td>
        <td align="center"><?php echo $ar['Price']?></td>
       <td align="center">
       <select class="myclass" name="qty" id='qty<?php echo $cnt;?>'>
       <?php for ($i=1; $i<=100; $i++) {
               $y="";
               if( $i==$_SESSION['r'][$key]['Quantity']){
                $y="selected";
                }
                echo "<option ";
                echo "value=\"$i\" ".$y.">", $i, "</option>\n";


            } ?>

       </select>
       </td>

        <?php $x=$x+$ar['Quantity']; ?>

         <td name="price" id="price"><span class="priceDisplay<?php echo $cnt;?>"><?php echo $ar['Total']; ?></span></td>
    <!--   <td id="tot" align="center"><?php// echo $ar['Total'] ?></td> -->
        <input type='hidden' name='id' id="pid" value='<?php echo $ar['Id']; ?>'>
        <input type='hidden' name='price' id="price" value='<?php echo $ar['Price'] ?>' class="input">  
       <td><input type='checkbox' name='remove' value='Remove' onCheck="$(this).closest('tr').remove();">

Here, my id for drop down list in each row is different.It is generated automatically as the loop continues. 在这里,我每一行的下拉列表ID是不同的,它会在循环继续时自动生成。 All I need is that, when the user changes a product's quantity corresponding price has to be changed. 我需要的是,当用户更改产品数量时,必须更改相应的价格。 The thing is that, when the user changes the quantity, jquery has to be invoked.It should pass the select id. 事实是,当用户更改数量时,必须调用jquery,它应该传递选择ID。 But my select id is different for each product. 但是每种产品的选择ID都不相同。 So how I achieve it? 那么我如何实现呢? Please help me. 请帮我。

您是否尝试过在计算总价格的select上实现onChange()函数?您可以将一个类添加到价格和数量的容器中,以进行区分,然后遍历它们。

Try this 尝试这个

<td align="center" id="price"><?php echo $ar['Price']?></td>
       <td align="center">

<select class="myclass" name="qty" id='qty<?php echo $cnt;?>' onchange="get_price(this)">
       <?php for ($i=1; $i<=100; $i++) {
               $y="";
               if( $i==$_SESSION['r'][$key]['Quantity']){
                $y="selected";
                }
                echo "<option ";
                echo "value=\"$i\" ".$y.">", $i, "</option>\n";


            } ?>

       </select>

<script>
function get_price(val)
{
    var qty = $(val).val();
    var price = $("#price").html();
    var price =  qty * price;
    $("#price").html(price);
    return false;


}

</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM