【发布时间】:2016-12-22 11:23:24
【问题描述】:
我正在尝试在 Magento 分组项目中添加增量,代码应如下但 javascript 返回 TypeError: qty_el is null。 我认为问题应该是当我传递元素 id 变量时,但无法解决这个问题。 我错过了什么?还有其他方法可以做到这一点吗? 希望有人可以帮助我。
<div class="add-to-cart">
<div class="qty-button form-group">
<input type="text" name="super_group_<?php echo $_item->getId() ?>" id="super_group_<?php echo $_item->getId() ?>" maxlength="12" value="<?php echo $_item->getQty() * 1 ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Qty')) ?>" class="input-text qty group-qty form-control" />
<div class="box-container">
<div class="box-icon button-plus">
<input type="button" onclick="incre(2,'super_group_<?php echo $_item->getId() ?>')" class="qty-increase" />
</div>
<div class="box-icon button-minus">
<input type="button" onclick="decre(<?php echo json_encode ($i);?>,'super_group_<?php echo $_item->getId() ?>')" class="qty-decrease" />
</div>
</div>
</div>
</div>
<script type="text/javascript">
function incre(qty_inc,idname)
{
var qty_el = document.getElementById(idname);
var qty = qty_el.value;
if( !isNaN( qty )){
if(qty_inc>0){
qty_el.value = Number(qty) + qty_inc ;
} else {
qty_el.value++;
}
}
}
function decre(qty_inc,idname)
{
var qty_el = document.getElementById(idname);
var qty = qty_el.value;
if(!isNaN( qty ) && qty > '0') {
if(qty_inc>0){
qty_el.value = Number(qty) - qty_inc;
} else {
qty_el.value--;
}
}
}
【问题讨论】:
-
只需将 alert(idname) 添加到 incre() 中,看看你得到了什么?
标签: javascript php magento increment