【问题标题】:Adding increment to form input getting TypeError: qty_el is null向表单输入添加增量得到 TypeError: qty_el is null
【发布时间】: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


【解决方案1】:

您可以尝试另一种方法来增加分组产品的减量

<label for="qty"><?php echo $this->__('Quantity:') ?></label>
<a href="javascript:void(0);" onclick="var qty_el = document.getElementById('super_group_<?php echo $_item->getId(); ?>'); var qty = qty_el.value; if( !isNaN( qty ) && qty > 1 ) qty_el.value--;return false;" class="qty-decrease" > - </a>
<input type="text" pattern="\d*" name="super_group[<?php echo $_item->getId() ?>]" id="super_group_<?php echo $_item->getId(); ?>" maxlength="12" value="<?php echo max($this->getProductDefaultQty() * 1, 1) ?>" title="<?php echo $this->__('Quantity') ?>" class="input-text qty" />
<a href="javascript:void(0);" onclick="var qty_el = document.getElementById('super_group_<?php echo $_item->getId(); ?>'); var qty = qty_el.value; if( !isNaN( qty )) qty_el.value++;return false;" class="qty-increase" /> + </a>

【讨论】:

    猜你喜欢
    • 2020-09-01
    • 2013-05-25
    • 2011-07-11
    • 1970-01-01
    • 2023-04-04
    • 2015-09-18
    • 1970-01-01
    • 2014-10-13
    • 2021-08-25
    相关资源
    最近更新 更多