如果我单击向上箭头或向下箭头,则升级了此功能以更新数量编号。在数量编号旁边有一个提交按钮之前,它将进行更新。但是现在,如果我有2个购物车项目,它将2个ID混合在一起,然后创建一个新的购物车项目,而不是更新1个ID号的数量。如何使用以下代码解决此问题。
$('#quantity').on('input', function() {
// get basic information for updating the cart
var id = $('.update-quantity-form').find('.product-id').text();
var quantity = $('.update-quantity-form').find('.cart-quantity').val();
// redirect to update_quantity.php, with parameter values to process the request
window.location.href = "update_quantity.php?id=" + id + "&quantity=" + quantity;
return false;
});
});
This is the php/html code:
//update quantity
echo "<form class='update-quantity-form'>";
echo "<div class='product-id' style='display:none;'>{$id}</div>";
echo "<div class='input-group'>";
echo "<input id='quantity' type='number' name='quantity' value='{$quantity}' class='form-control cart-quantity' min='1' />";
echo "</div>";
echo "</form>";
因此,如果我有产品ID号40和产品号41,则它们都像id = 4041。我该如何更改此方法,使其仅在增加数量时才使用产品ID。
答案 0 :(得分:0)
$('#quantity').on('input', function() {
// get basic information for updating the cart
var id = $(this).attr('rel');
var quantity = $(this).val();
// redirect to update_quantity.php, with parameter values to process the request
window.location.href = "update_quantity.php?id=" + id + "&quantity=" + quantity;
return false;
});
<form class='update-quantity-form'>
<div class='input-group'>
<input id='quantity' type='number' name='quantity' rel='{id}' value='{quantity}' class='form-control cart-quantity' min='1' />
</div>
</form>
您可以在这里js fiddle
进行测试