Closed. This question needs debugging details。它当前不接受答案。
                        
                    
                
            
        
            
        
                
                    
                
            
                
                    想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                
                    5年前关闭。
            
        

    

我正在购物车上。我要将产品添加到购物车中。然后使用Ajax更改项目“更改数量”。此过程完全适用于“首次更改”。但是当我再次更改数量时,该部分没有刷新。

这是显示购物车详细信息的代码

        <?php
        $session_id = $_SESSION["session_id"];
        $query = "select c.cart_id, c.item_quantity, p.category_name, p.product_thumb, p.reduced_price from cart as c inner join category as p on c.category_id = p.category_id where c.session_id = '$session_id'";

        $select_cart = mysqli_query($mysqli, $query);
        if (mysqli_num_rows($select_cart) > 0)
        {
        ?>
        <table>
            <tr>
                <th>Item</th><th>Item Detail</th><th>Quantity</th><th>Price</th><th>Sub Total</th>
            </tr>
            <?php
            while ($result_cart = mysqli_fetch_object($select_cart))
            {
            ?>
            <tr id="cart<?php echo $result_cart->cart_id; ?>">
                <td width="100" align="center"><img src="images/products/<?php echo $result_cart->product_thumb; ?>" width="50" /></td>
                <td><?php echo $result_cart->category_name; ?><br /><?php echo $result_cart->product_name; ?></td>

                <td width="50" align="center">

                    <select class="quantity" id="quantity<?php echo $result_cart->cart_id; ?>" >
                        <?php

                        for ($i = 1; $i <= 10; $i++)
                        {

                        ?>
                        <option value="<?php echo $i; ?>" <?php if ($result_cart->item_quantity == $i) { echo "selected"; } ?>><?php echo $i; ?></option>
                        <?php
                        }
                        ?>

                    </select>

                </td>

                <td width="75" align="center">$<?php echo $result_cart->reduced_price; ?></td>
                <td width="100" align="center">$<?php echo $result_cart->item_quantity * $result_cart->reduced_price; ?>
                <a title="Delete" href="delete-cart.php?id=<?php echo $result_cart->cart_id; ?>" class="delete"></a></td>

            </tr>
            <?php
            }
            ?>
        </table>
        <?php
        }
        else
        {
        ?>
        <p>Cart is Empty..!</p>
        <?php
        }
        ?>


这是在同一页面上使用的Ajax脚本

<script type="text/javascript">
$('.quantity').change( function()
{
var ID = $(this).attr("id");
var sid=ID.split("quantity");
var New_ID=sid[1];

var QTY = document.getElementById(ID).value;

var URL='updatecart.php';
var dataString = 'id=' + New_ID +'&qty='+ QTY;


$.ajax({
type: "GET",
url: URL,
data: dataString,
cache: false,
 success: function(data){
     $('#cart'+New_ID).html(data);
     alert (data);

}
});
});
</script>


这是我将数据更新到database-updatecart.php中的代码。

<?php
include('admin/includes/config.php');

if(isset($_GET['id'])) {

$cart_id = ($_GET['id']);
$item_quantity = ($_GET['qty']);

mysqli_query($mysqli, "update cart set item_quantity = '$item_quantity' where cart_id = '$cart_id'");

// Loop through the products and output HTML for JavaScript to use
?>
<?php
$query = "select c.cart_id, c.item_quantity, p.category_name, p.product_thumb, p.reduced_price             from cart as c inner join category as p on c.category_id = p.category_id where c.cart_id = '$cart_id'";
$select_cart = mysqli_query($mysqli, $query);
$result_cart = mysqli_fetch_object($select_cart);
?>

                <td width="100" align="center"><img src="images/products/<?php echo $result_cart->product_thumb; ?>" width="50" /></td>
                <td><?php echo $result_cart->category_name; ?><br /><?php echo $result_cart->product_name; ?></td>

                <td width="50" align="center">

                    <select class="quantity" id="quantity<?php echo $result_cart->cart_id; ?>" >
                        <?php

                        for ($i = 1; $i <= 10; $i++)
                        {

                        ?>
                        <option value="<?php echo $i; ?>" <?php if ($result_cart->item_quantity == $i) { echo "selected"; } ?>><?php echo $i; ?></option>
                        <?php
                        }
                        ?>

                    </select>

                </td>

                <td width="75" align="center">$<?php echo $result_cart->reduced_price; ?></td>
                <td width="100" align="center">$<?php echo $result_cart->item_quantity * $result_cart->reduced_price; ?>
                <a title="Delete" href="delete-cart.php?id=<?php echo $result_cart->cart_id; ?>" class="delete"></a></td>

<?php
}
?>


我需要在更改特定项目的数量时自动计算购物车的小计价格和总价。我是Ajax的新手。

这些是类似的问题。

1. Ajax form only work one time

2. Ajax Request works only once - code Ignitor

提及这些问题后,我尝试使用类似$('.quantity').on( "change", "select", function() {的方法,但未获得结果。

请帮我。我是否需要更改整个编码结构。

最佳答案

在ajax成功之后,您将更改DOM(使用html()方法)。该元素将替换为新元素,因此您的事件将被删除。

另外,您的其他解决方案仅将更改应用于也已删除的数量元素。尝试类似

$(document).on( "change", ".quantity", function() {}

07-28 02:50
查看更多