Javascript 根据所选产品从数据数组填充价格

Javascript 根据所选产品从数据数组填充价格,javascript,jquery,autocomplete,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

我有一个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>
请帮助我如何填充产品1的price1 id和产品2的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)
        });
}       


});
也许你会接近这个决定。例子:

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

$( "#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...
    });
}

你到底想做什么?用json数组中给出的产品的完整列表填充每个select?或者每个select都用该数组的相应值设置,例如product1将在数组中选择id=1,product2将选择id=2,…?如果用户选择product 1,我希望为price1设置值,如果用户选择product 7,我必须从json数组中设置product 7的值,因为id和标签为7,并且该元素的值为。请帮忙!我选择的产品在价格上没有任何变化。它应该填充价格框中的产品价格:请帮助感谢$'product1'。changefunction{$'input[name=unit_price1]'。valdata[$this.val-1].value};工作位我希望在数据结束前对此进行for循环。感谢$'product1'.changefunction{$'input[name=unit_price1]'.valdata[$this.val-1].value};工作位我希望在数据结束前对此进行for循环。如果您使用数据[$this.val-1].value,但该id字段不再等于$this.val-1,这将不再有效…对于消息的第二部分,我不明白您为什么要循环?在哪个数组上?哦,好的,我看到了你的编辑。。。但您必须以ipproduct=1开始循环。看我更新的答案!
$( "#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...
    });
}