我需要显示多个选择框中文本的总和 - 而不是值,例如。
<select class="summable1">
<option value="blue">1</option>
<option value="green">2</option>
<option value="black">3</option>
</select>
<select class="summable2">
<option value="orange">1</option>
<option value="pink">2</option>
<option value="purple">3</option>
</select>
<select class="summable3">
<option value="yellow">1</option>
<option value="red">2</option>
<option value="brown">3</option>
</select>
<div id="result">The sum of the selected text 1,2,3 - not the values</div>
答案 0 :(得分:0)
尝试这样的事情
var $select=$('.summable1,.summable2,.summable3');
$select.on('change',function(){
var sum=0;
$select.each(function(){
sum+=parseInt($(this).find('option:selected').text(),10);
})
$('#result').html(sum);
});
<强> FIDDLE DEMO 强>
答案 1 :(得分:0)
var val1 = parseInt($(".summable1").eq(0).children("option:selected").text());
var val2 = parseInt($(".summable2").eq(0).children("option:selected").text());
var val3 = parseInt($(".summable3").eq(0).children("option:selected").text());
你应该创造一些更有活力的东西但你明白了