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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?