我想在产品列表中添加数量的增加和减少。这是我在Prestashop 1.6中的代码:
<script type="text/javascript">// The button to increment the product value
$(document).on('click', '.product_quantity_up', function(e){
e.preventDefault();
fieldName = $(this).data('field-qty');
var currentVal = parseInt($('input[name='+fieldName+']').val());
if (!allowBuyWhenOutOfStock && quantityAvailable > 0)
quantityAvailableT = quantityAvailable;
else
quantityAvailableT = 100000000;
if (!isNaN(currentVal) && currentVal < quantityAvailableT)
$('input[name='+fieldName+']').val(currentVal + 1).trigger('keyup');
else
$('input[name='+fieldName+']').val(quantityAvailableT);
$('#quantity_wanted').change();
});
// The button to decrement the product value
$(document).on('click', '.product_quantity_down', function(e){
e.preventDefault();
fieldName = $(this).data('field-qty');
var currentVal = parseInt($('input[name='+fieldName+']').val());
if (!isNaN(currentVal) && currentVal > 1)
$('input[name='+fieldName+']').val(currentVal - 1).trigger('keyup');
else
$('input[name='+fieldName+']').val(1);
$('#quantity_wanted').change();
});</script>
<div class="hide quantity_wanted_flatlist">
<p id="quantity_wanted_p">
<input type="number" min="1" name="qty" id="quantity_wanted" class="text" value="1" />
<a href="#" data-field-qty="qty" class="btn btn-default button-minus product_quantity_down">
<span><i class="icon-minus"></i></span>
</a>
<a href="#" data-field-qty="qty" class="btn btn-default button-plus product_quantity_up">
<span><i class="icon-plus"></i></span>
</a>
<span class="clearfix"></span>
</p>
</div>
就像你看到我有js和输入文件名为quantity_wanted。当我按+或 - 什么都没发生。这是源代码。
答案 0 :(得分:0)
根据您分享的链接,您的代码正常运行。数量递增和递减。但是当我们点击任何相应产品的任何( - )或(+)按钮时,数量变化反映在所有产品中。
这种情况的发生只是因为当您更新任何产品的数量时,您已为所有输入字段指定了相同的ID名称,然后更新反映在所有产品中。
解决方案:
// The button to increment the product value
$(document).on('click', '.product_quantity_up', function(e){
e.preventDefault();
fieldName = $(this).data('field-qty');
var currentVal = parseInt($('input[name='+fieldName+']').val());
if (!allowBuyWhenOutOfStock && quantityAvailable > 0)
quantityAvailableT = quantityAvailable;
else
quantityAvailableT = 100000000;
if (!isNaN(currentVal) && currentVal < quantityAvailableT)
$(this).closest('input[name='+fieldName+']').val(currentVal + 1).trigger('keyup');
else
$(this).closest('input[name='+fieldName+']').val(quantityAvailableT);
});
// The button to decrement the product value
$(document).on('click', '.product_quantity_down', function(e){
e.preventDefault();
fieldName = $(this).data('field-qty');
var currentVal = parseInt($('input[name='+fieldName+']').val());
if (!isNaN(currentVal) && currentVal > 1)
$(this).closest('input[name='+fieldName+']').val(currentVal - 1).trigger('keyup');
else
$(this).closest('input[name='+fieldName+']').val(1);
});
以上代码将找到名称数量的较近数量输入框(从&#34; field-qty&#34;按钮属性获得)并设置计算数量。