0

私は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 );
            }
        });

price1 id product 1 と product2 の 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)
        });
}       


});
4

2 に答える 2

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/ </p>

于 2012-11-23T10:41:13.927 に答える
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...
    });
}
于 2012-11-23T10:59:14.837 に答える