Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Javascript修改表单值_Javascript_Php_Jquery_Forms - Fatal编程技术网

使用Javascript修改表单值

使用Javascript修改表单值,javascript,php,jquery,forms,Javascript,Php,Jquery,Forms,我正在尝试向数量字段添加递增/递减按钮。因此,当用户按下向右箭头时,“数量”字段中的数量将更新1。这是我的代码,我得到了一个未捕获的引用错误:在HtmlanchoreElement.onclick中没有定义incrementQty,我不确定如何继续。函数存在并且我正在传递动态表单id <script type=”text/javascript”> function decrementQty(id) { var val = parseInt($('#' + id).

我正在尝试向数量字段添加递增/递减按钮。因此,当用户按下向右箭头时,“数量”字段中的数量将更新1。这是我的代码,我得到了一个未捕获的引用错误:在HtmlanchoreElement.onclick中没有定义incrementQty,我不确定如何继续。函数存在并且我正在传递动态表单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>

函数递减率(id){
var val=parseInt($('#'+id).getValue();
如果(val!=1)
document.getElementById(id).value=val-1;
}
函数增量数量(id){
var val=parseInt($('#'+id).getValue();
document.getElementById(id).value=val+1;
}

为什么将函数包装在
CDATA
标记中?共享完整呈现的HTML,而不是PHP代码。PHP代码块很难读取。为什么不改用
heredoc
?一半使用jQuery,一半使用javascript。天啊。更改此
type=“text/javascript”
$(id).val()
您在javascript函数中错过了按id元素的jQuery选择器的递减性和递增数量:
var val=parseInt($('#'+id).getValue())
<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>
<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>