0

如何获取一组下拉列表并将更改的值添加到文本字段中?

<select name="qty[1]" id="qty[1]">
<option selected="selected">500</option>
<option>1000</option>
<option>1500</option>
<option>2000</option>
<option>2500</option>
</select>

<select name="qty[2]" id="qty[2]">
<option selected="selected">500</option>
<option>1000</option>
<option>1500</option>
<option>2000</option>
<option>2500</option>
</select>

<input type="text" name="total_qty" id="total_qty" />

因此,如果我选择 1000,则总 txtbox 将更改为 1000,然后如果我从另一个选择框中选择 2000,则总数应更改为 3000

4

2 回答 2

1

用这个:

var selects = $('select[name^=qty]');
selects.change(function(){
    var value = 0;
    selects.each(function(){ value += +this.value; });
    $('#total_qty').val(value);
}).trigger('change');

它适用于两个<select>或二十个盒子,而无需更改代码。

它在这里工作:http: //jsfiddle.net/eYmF8/2/

于 2013-06-27T19:45:55.237 回答
0

Javascript:

var qty1 = document.getElementById("qty[1]");
var qty2 = document.getElementById("qty[2]");
var total_qty = document.getElementById("total_qty");
qty1.onchange = function() {
  var total = parseInt(qty1.value) + parseInt(qty2.value);
  total_qty.value = total;
}
qty2.onchange = function() {
  var total = parseInt(qty1.value) + parseInt(qty2.value);
  total_qty.value = total;
}

http://jsfiddle.net/SGY5E/

于 2013-06-27T19:45:52.473 回答