Jquery Concat JSON对象无法正常工作

时间:2015-12-01 10:04:12

标签: javascript jquery json

我想创建产品数量字段。我已经完成它工作正常但是当我在代码中添加数字ID时,它无法正常工作。请查看代码,希望您能轻松理解我的问题。

HTML CODE

<input type='button' value='-' id="qtyMinus<?php echo $product['cart_id']; ?>" class='qty-minus qone01' field='quantity' />
<input type='text' name="quantity[<?php echo $product['cart_id']; ?>]" value="<?php echo $product['quantity']; ?>" class='shopping-qty qone02' />
<input type='button' value='+' id="qtyPlus<?php echo $product['cart_id']; ?>" class='qty-plus' field='quantity' />

JS代码

 var productIds =  [{"id":"13"},{"id":"14"},{"id":"15"}];


            $(jQuery.parseJSON(JSON.stringify(productIds))).each(function() {  
                var ID = this.id;
                $('#qtyPlus' + ID).click(function(e){
                    // Stop acting like a button
                    e.preventDefault();
                    // Get the field name
                    fieldName = $(this).attr('field');
                    // Get its current value
                    var currentVal = parseInt($('input[name='+fieldName+']').val());
                    // If is not undefined
                    if (!isNaN(currentVal)) {
                    // Increment
                    $('input[name='+fieldName+']').val(currentVal + 1);
                    } else {
                    // Otherwise put a 0 there
                    $('input[name='+fieldName+']').val(0);
                    }
                });
                // This button will decrement the value till 0
                $('#qtyMinus' + ID).click(function(e) {
                    // Stop acting like a button
                    e.preventDefault();
                    // Get the field name
                    fieldName = $(this).attr('field');
                    // Get its current value
                    var currentVal = parseInt($('input[name='+fieldName+']').val());
                    // If it isn't undefined or its greater than 0
                    if (!isNaN(currentVal) && currentVal > 0) {
                    // Decrement one
                    $('input[name='+fieldName+']').val(currentVal - 1);
                    } else {
                    // Otherwise put a 0 there
                    $('input[name='+fieldName+']').val(0);
                    }
                });

            });

我需要为每个输入字段动态js代码,但循环不能正常工作。但是当我在循环中警告然后它工作并显示所有ID。请指导我哪里错了。

0 个答案:

没有答案