[英]Ajax Increasing Quantity on Cart only Work Once
I have a page to increasing and decreasing quantity product on cart before going to checkout confirmation page.在进入结帐确认页面之前,我有一个页面可以增加和减少购物车上的产品数量。 Actually im doing with ajax, and then the back end will execute manipulate quantity of product based on button i clicked (it has product_id).实际上我是用ajax做的,然后后端将根据我点击的按钮(它有product_id)执行操作数量的产品。
The problem is, for the first time the ajax runs well and then i refresh the table (not whole page).问题是,第一次 ajax 运行良好,然后我刷新了表格(不是整个页面)。 But, when i click the button again.但是,当我再次单击按钮时。 It returns nothing.它什么都不返回。 BUT, after i refresh page using F5 and then click the button again, the quantity is updated.但是,在我使用 F5 刷新页面然后再次单击该按钮后,数量会更新。
Could you please show me the correct ways to solve this problem?你能告诉我解决这个问题的正确方法吗? (PS: Im sorry for my english) (PS:对不起我的英语)
Ajax call :阿贾克斯调用:
$(document).ready(function () {
//Increase quantity on cart
$(".btnPlus").on('click', function () {
var id = $(this).val();
var url = "CRUD member/update-checkout-plus.php";
var postdata = {"id": id};
$.post(url, postdata, function (html) {
$("#myCart").load(location.href + " #myCart");
});
});
Here is the button, im only working for the button plus, the minus button i havent do that yet.这是按钮,我只为加号按钮工作,减号按钮我还没有这样做。
echo '<td style="text-align: center">'
. '<button class="btnPlus" value="' . $item['id'] . '"><span class="glyphicon glyphicon-plus"></span></button>'
. '<input type="text" class="fieldQty" value="' . $item['qty'] . '" style="text-align: center" size="2" readonly/>'
. '<button class="btnMinus" value="' . $item['id'] . '"><span class="glyphicon glyphicon-minus"></span></button>'
. '</td>';
Backend (update-checkout-plus.php) :后端(update-checkout-plus.php):
include '../../config.php';
$id = $_POST['id'];
$query = mysql_query("SELECT stock FROM products WHERE product_id = '$id'");
$row = mysql_fetch_array($query);
$stock = $row['stock'];
//if the quantity has reached maximum of stock (DB)
//quantity == $stock
//else quantity++
if ($_SESSION['cart'][$id]['qty'] >= $stock) {
$_SESSION['cart'][$id]['qty'] = $stock;
} else {
$_SESSION['cart'][$id]['qty'] ++;
}
What seems to me that you have a class .btn
which resides in #mycart
div, and every time you .load()
you change the DOM so new elements gets in the div and that causes the old bindings gets removed from the DOM. 在我看来,你有一个类.btn
驻留在#mycart
div中,每次你.load()
你改变DOM,所以新的元素进入div,这导致旧的绑定从DOM中删除。
So in this case you have to delegate the event to the closest static parent / document / body: 因此,在这种情况下,您必须将事件委托给最近的静态父/文档/正文:
$(document).on('click', '.btnPlus', function () {
Call this function,where you append .btnPlus 调用此函数,在其中追加.btnPlus
function bindAddToCart(){
$(document).on('click', '.btnPlus', function () {
//your click functionality
});
}
add location.reload() look at the code below:添加 location.reload() 看下面的代码:
$('.cart_quantity_up').click(function(){
var id=$(this).attr("pid").toString();
var pls=this.parentNode.children[1]
console.log(pls)
console.log(id)
$.ajax({
type:"GET",
url:"/pluscart/",
data:{
prod_id:id
},
success:function(data){
pls.innerText=data.quantity
location.reload()
}
})
});
Go to YourTheme/templates/checkout/cart.phtml and paste the below code: 转到YourTheme / templates / checkout / cart.phtml并粘贴以下代码:
<script type="text/javascript">
function changeItemQuantity( qty,num,cartid) {
var num = num;
var cartid = cartid;
var quantity = document.getElementById(cartid).value
/* Restrict Quantity as a Non Negative */
quantity = Math.max(1, quantity);
var currentVal = parseInt(quantity);
var final_val = currentVal + num;
document.getElementById(cartid).value=final_val;
}
</script>
Go to app/design/frontend/YourTheme/default/template/checkout/cart/item/default.phtml and paste this code around line 207: 转到app / design / frontend / YourTheme / default / template / checkout / cart / item / default.phtml并在第207行粘贴此代码:
<a class="mobile-only" onclick="changeItemQuantity(<?php echo $this->getQty() ?>,-1,<?php echo $_item->getId()?>); return false;" href="#"> - </a>
<label class="mobile-only m-text"><?php echo $this->__('QTY') ?></label>
<input name="cart[<?php echo $_item->getId() ?>][qty]" value="<?php echo $this->getQty() ?>" size="4" title="<?php echo $this->__('Qty') ?>" id="<?php echo $_item->getId()?>" class="input-text qty" maxlength="12" />
<a class="mobile-only" onclick="changeItemQuantity(<?php echo $this->getQty() ?>,1,<?php echo $_item->getId()?>); return false;" href="#"> + </a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.