dqu94359 2018-04-16 03:11
浏览 63
已采纳

使用Javascript修改表单值

I am trying to add increment/decrement buttons to the quantity field. So when a user presses the right arrow the quantity in the qty field will update by 1. Here is my code, I am getting a Uncaught ReferenceError: incrementQty is not defined at HTMLAnchorElement.onclick and am not sure how to proceed. The function exists and I am passing the dynamic form id?

<script type=”text/javascript”>
     function decrementQty(id) {
     var val = parseInt($('#' + id).getValue());
     if(val != 1)
     document.getElementById(id).value = val - 1;
     }
     function incrementQty(id) {
     var val = parseInt($('#' + id).getValue());
     document.getElementById(id).value = val + 1;
     }
</script>


<input id="cart-<?php /* @escapeNotVerified */ echo $_item->getId() ?>-qty"
                           name="cart[<?php /* @escapeNotVerified */ echo $_item->getId() ?>][qty]"
                           data-cart-item-id="<?php /* @escapeNotVerified */ echo $_item->getSku() ?>"
                           value="<?php /* @escapeNotVerified */ echo $block->getQty() ?>"
                           type="number"
                           size="4"
                           title="<?php echo $block->escapeHtml(__('Qty')); ?>"
                           class="input-text qty"
                           maxlength="12"
                           data-validate="{required:true,'validate-greater-than-zero':true}"
                           data-role="cart-item-qty"/>
<a class="pic arrow-right qty" href="javascript:void(0);" onclick="incrementQty('cart-<?php /* @escapeNotVerified */ echo $_item->getId() ?>-qty');"></a>

Rendered HTML

<input id="cart-502-qty"
                           name="cart[502][qty]"
                           data-cart-item-id="7640149080453"
                           value="8"
                           type="number"
                           size="4"
                           title="Qty"
                           class="input-text qty"
                           maxlength="12"
                           data-validate="{required:true,'validate-greater-than-zero':true}"
                           data-role="cart-item-qty"/>
<a class="pic arrow-right qty" href="javascript:void(0);" onclick="incrementQty('cart-502-qty');"></a>
  • 写回答

1条回答 默认 最新

  • dragonsun00000 2018-04-18 04:42
    关注
    <input id="cart-<?php /* @escapeNotVerified */ echo $_item->getId() ?>-qty"
                               name="cart[<?php /* @escapeNotVerified */ echo $_item->getId() ?>][qty]"
                               data-cart-item-id="<?php /* @escapeNotVerified */ echo $_item->getSku() ?>"
                               value="<?php /* @escapeNotVerified */ echo $block->getQty() ?>"
                               type="number"
                               size="4"
                               title="<?php echo $block->escapeHtml(__('Qty')); ?>"
                               class="input-text qty"
                               maxlength="12"
                               data-validate="{required:true,'validate-greater-than-zero':true}"
                               data-role="cart-item-qty"/>
                    <a class="pic arrow-right qty" href="javascript:void(0);" onclick="increaseQTY('cart-<?php /* @escapeNotVerified */ echo $_item->getId() ?>-qty');"></a>
    
    <script>
    
    function increaseQTY(formid) {
        document.getElementById(formid).value ++;
    }
    
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stc15f2k60s2单片机关于流水灯,时钟,定时器,矩阵键盘等方面的综合问题
  • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?
  • ¥15 django5安装失败
  • ¥15 Java与Hbase相关问题
  • ¥15 后缀 crn 游戏文件提取资源
  • ¥20 bash代码推送不上去 git fetch origin master #失败了
  • ¥15 LOL外服加入了反作弊系统,现在游戏录像rofl文件离线都无法打开