我在一个页面上有几个下拉列表,它们都包含一些数字。当用户更改其中任何一个时,我需要遍历所有这些并对所有数字求和。
我将如何使用 jQuery 来做到这一点?
<select id="number1">
<option value="1">1</option>
<option value="3">3</option>
</select>
<select id="number2">
<option value="1">1</option>
<option value="3">3</option>
</select>
<select id="number4">
<option value="1">1</option>
<option value="3">3</option>
</select>
我试过类似的东西:
$('select[id*="number"].change(function() { // when any one dropdown change
$('select[id*="number"].change(function() { // go through them all
var current = $(this);
// add current value to array
});
});
$.each(arr,function() {
total += this; // sum items
});
但它并不像预期的那样工作。有小费吗?谢谢。