我已将此代码写入购物车的4 +和 - 按钮。但我必须通过在输入字段中手动输入数字来更改数量
<div class="quantity-stepper hidden-xs">
<button class="less js-less" onclick="changeQty('${commerceItemId}','-', ${status.index});">-</button>
<input type="text" id="quantityToChange${status.index}" class="product-value" value="${currentItem.quantity}" />
<button class="more js-more" onclick="changeQty('${commerceItemId}','+', ${status.index});">+</button>
</div>
<script type="text/javascript">
$(document).ready(function() {
var quantityProdAry = $( "input[id*='quantityToChange']" );
$.each(quantityProdAry, function(key, value) {
if(parseInt(value.value) > 0 ) {
$('.js-less').prop('disabled', false);
}
else {
$('.js-less').prop('disabled', true);
}
});
});
function changeQty(commerceItem,operation, index){
var quantity;
var prevQuantity = parseInt($('#quantityToChange'+index).val());
var index = parseInt(index);
if(operation == '+') {
quantity = (prevQuantity*1) + 1;
if(parseInt(quantity) > 0 ) {
$('.js-less').prop('disabled', false);
}
else {
$('.js-less').prop('disabled', true);
}
}
else {
if(parseInt(prevQuantity) > 0 ) {
$('.js-less').prop('disabled', false);
quantity = (prevQuantity*1) - 1;
}
else {
$('.js-less').prop('disabled', true);
}
}
$('#value'+commerceItem+'index'+index).val(quantity);
//$('#quantityToChange'+index).val(quantity)
$('#changeQty').click();
}
</script>