2015-02-23 68 views
0

我是Magento的新手。请原谅我问这个问题,但我需要真正的帮助,因为我无法弄清楚。为什么每次单击增加或减少按钮时,Box会增加2个按钮

我在magento中使用了classishop主题。我的问题是我已经在list.phtml页面上添加了添加到CART按钮和数量框。我做了以下任务:

第一我已经加入这个:

<div class="quantity"> 

<input type="button" value="" id="add1" class="plus" title="<?php echo $this->__('Increments here No. of Qty') ?>" onclick="setQty(<?php echo $_product->getId(); ?>, '<?php echo $this->getAddToCartUrl($_product) ?>');"/> 
<input type="text" name="qty_<?php echo $_product->getId(); ?>" id="qty_<?php echo $_product->getId(); ?>" maxlength="12" value="<?php echo $this->getMinimalQty($_product) ?>" onkeyup="setQty(<?php echo $_product->getId(); ?>, '<?php echo $this->getAddToCartUrl($_product) ?>');" title="<?php echo $this->__('Input here No. of Qty') ?>" class="input-text qty" /> 
<input type="button" value="" id="minus1" class="minus" title="<?php echo $this->__('Decrement here No. of Qty') ?>" onclick="setQty(<?php echo $_product->getId(); ?>, '<?php echo $this->getAddToCartUrl($_product) ?>');"/> 
</div> 
<span id="cart_button_<?php echo $_product->getId(); ?>"><button type="button" class="addToCart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"></span></button></span> 
</form> 

我加了jQuery代码的第二件事:

<script> 
jQuery.noConflict(); 
jQuery(".plus").click(function() 
{ 
     var currentVal = parseInt(jQuery(this).next(".qty").val()); 
     if (!currentVal || currentVal=="" || currentVal == "NaN") currentVal = 1; 
        jQuery(this).next(".qty").val(currentVal + 1); 

    }); 

    jQuery(".minus").click(function() 
    { 
     var currentVal = parseInt(jQuery(this).prev(".qty").val()); 
     if (currentVal == "NaN") currentVal = 0; 
     if (currentVal > 1) 
     { 
      jQuery(this).prev(".qty").val(currentVal - 1); 
     } 
     // Ajax save here?? 
    }); 

</script> 
<script type="text/javascript"> 
function setQty(id, url) { 
var qty = document.getElementById('qty_' + id).value; 
document.getElementById('cart_button_' + id).innerHTML = '<button type="button" class="addToCart" onclick="setLocation(\'' + url + 'qty/' + qty + ')"></button>'; 
} 
</script> 

的问题是,当我点击+或 - 按钮,然后数量框值增加或减少2.请帮我解决这个问题。提前致谢。

+0

我把它变成一个小提琴和它的正常工作,这个问题必须在别的地方在你的代码 - http://jsfiddle.net/1p97me9d/ – Bradley4 2015-02-25 04:44:44

+0

我找到了解决办法。我只是将脚本放入footer.phtml文件并且工作正常 – user3785854 2015-02-25 17:25:01

回答

0

我找到了一个解决方案,因为我将jquery脚本放入footer.phtml文件中,因为它运行了2次。

现在,它的做工精细