我在class=pricefield
的div中创建了动态选择框。我想获取jQuery中每个选择框中选择了哪个选项的所有选择框值。
<div class="pricefield">
// Now all comes dynamically
<label>first</label>
<div class=""d-field>
<select>
<option value="100">first</option>
<option value="200">secound</option>
//may be more options
</select>
</div>
<label>second</label>
<div class=""d-field>
<select>
<option value="300">first</option>
<option value="400">second</option>
//Maybe more options
</select>
</div>
// Maybe more select
</div>
.js代码
jQuery('.pricefield select').each(jQuery(this).change(
function(key,value){
// It did not work.
alert(jQuery(this).filter("option:selected").val());
// It not worked, gives a blank value.
alert(jQuery(this).val());
// Key and value gives undefined.
alert(key);
alert(value);
}
));
我想在每个下拉列表中总结所选选项的所有值...
答案 0 :(得分:3)
写:
get_sum();
$('.pricefield select').change(function () {
get_sum();
});
function get_sum() {
var sum = 0;
$('.pricefield select').each(function () {
sum += parseInt($(this).find("option:selected").val());
});
alert(sum);
}