根据所选产品从数据阵列填充价格

时间:2012-11-23 10:27:41

标签: javascript jquery autocomplete

我有一个json数组

var data = [ { "id": "1", "label": 1, "value": "11.00" }, 
                 { "id": "2", "label": 2, "value": "21.00" }, 
                 { "id": "3", "label": 3, "value": "31.00" }, 
                 { "id": "4", "label": 4, "value": "40.00" }, 
                 { "id": "5", "label": 5, "value": "50.00" }, 
                 { "id": "6", "label": 6, "value": "60.00" }, 
                 { "id": "9", "label": 9, "value": "90.00" }, 
                 { "id": "8", "label": 8, "value": "80.00" }, 
                 { "id": "7", "label": 7, "value": "70.00" } 
               ];

和来自

<form action="" method="POST">
Quantity1: <input type="text" name="quantity1" id="quantity1">
Product1: <select name="product1" id="product1"><option value="1">Product 1</option><option value="2">Product 2</option></select>
Price1: <input type="text" name="price1" id="price">
Quantity2: <input type="text" name="quantity2" id="quantity2">
Product2: <select name="product1" id="product2"><option value="1">Product 1</option><option value="2">Product 2</option></select>
Price2: <input type="text" name="price2" id="price">
.
.
.
<input type="submit" value="submit">
</form>

我想自动填充产品的价格。我很抱歉我不了解jquery和javascript。 尝试了这个但没有奏效

$( "#product1" ).change(function(){
            source: data,
            minLength: 1,
            select: function( event, ui ) {
                 $('#price1').val( ui.item.value );
            }
        });

请帮助我如何填充product1的price1 id产品1和price2。


以下是#product1

的代码
$('#product1').change(function() {  
            $('input[name="unit_price1"]').val(data[$(this).val()-1].value)
        });

但是一旦我尝试循环,我的循环就不会出错了。

$(function() {
        var data = <?php echo $projects; ?>;
var nbProducts = data.length;
    for(var iProduct = 0; iProduct < nbProducts; iProduct++) {
        $('#product'+iProduct).change(function() {  
            $('input[name="unit_price'+iProduct+'"]').val(data[$(this).val()-1].value)
        });
}       


});

2 个答案:

答案 0 :(得分:1)

var data = [ { "id": "1", "label": 1, "value": "11.00" }, 
                 { "id": "2", "label": 2, "value": "21.00" }, 
                 { "id": "3", "label": 3, "value": "31.00" }, 
                 { "id": "4", "label": 4, "value": "40.00" }, 
                 { "id": "5", "label": 5, "value": "50.00" }, 
                 { "id": "6", "label": 6, "value": "60.00" }, 
                 { "id": "9", "label": 9, "value": "90.00" }, 
                 { "id": "8", "label": 8, "value": "80.00" }, 
                 { "id": "7", "label": 7, "value": "70.00" } 
               ];

for(var i=0; i<data.length; i++) {
    $('#product1').append('<option value="' + data[i].value + '">' + data[i].label+ '</option>');
    $('#product2').append('<option value="' + data[i].value + '">' + data[i].label+ '</option>');
}

$('#product1').change(function() {
    $('input[name="price1"]').val($(this).val())
});

$('#product2').change(function() {
    $('input[name="price2"]').val($(this).val())
});
​

也许你会接近这个决定。示例:http://jsfiddle.net/YvZ9v/

答案 1 :(得分:1)

您可以使用以下代码绑定change事件:

$( "#product1" ).change(function() {
    var nbProducts = data.length;
    for(var iProduct = 0; iProduct < nbProducts; iProduct++) {
        if(data[iProduct].id == this.value)
            $('#price1').val( data[iProduct].value );
    }
}

修改 为了遍历所有产品输入,你只需要迭代:

var nbProducts = data.length;
for(var iProduct = 1; iProduct <= nbProducts; iProduct++) {
    $('#product'+iProduct).change(function() {  
        // Same code as above...
    });
}