如何使用jquery对所选选项的值求和?

时间:2012-11-16 12:53:56

标签: jquery select sum option

您好我有3个选择器,所有选择器都有一些带有一些值的选项,如何对所选选项的所有值求和?

<select name='anch1'>
 <option value='10'>10 ++</option>
 <option value='20'>20 ++</option>
 <option value='30'>30 ++</option>
 <option value='40'>40 ++</option>
 <option value='50'>50 ++</option>
</select>

<select name='anch2'>
 <option value='10'>10 ++</option>
 <option value='20'>20 ++</option>
 <option value='30'>30 ++</option>
 <option value='40'>40 ++</option>
 <option value='50'>50 ++</option>
</select>

<select name='anch3'>
 <option value='10'>10 ++</option>
 <option value='20'>20 ++</option>
 <option value='30'>30 ++</option>
 <option value='40'>40 ++</option>
 <option value='50'>50 ++</option>
</select>

<div>SUM OF SELECTED OPTIONS</div>

2 个答案:

答案 0 :(得分:15)

$('select').change(function(){
    var sum = 0;
    $('select :selected').each(function() {
        sum += Number($(this).val());
    });
     $("#sum").html(sum);
});

http://jsfiddle.net/8mnQR/1/

答案 1 :(得分:0)

   var sum =  parseInt($('select[name="anch1"]').val()) +parseInt($('select[name="anch2"]').val()) +parseInt($('select[name="anch3"]').val() )

你可以这样做