-1

Thats it, how can I sum, some div class ID's when checkbox:checked with jquery?

Everytime I check a checkbox, I should be able to sum some ID's that came from some query that are related to each checkbox.

Eg. for 3 checked chekbox's

for every each checkbox:checked -> sum += $(".someclass").attr('id') 

My problem is: .attr(id) that save only the 1st found ID.

Imagine that first ID = 5.

my sum will be 5+5+5, and not ID(class value from checkbox1)+ID(class value from checkbox2)+ID(class value from checkbox3)

How could I do it ?

Thanks.

4

1 に答える 1

2

これで試してください

var sum = 0;
$(".someclass:checked").next().each(function(){
    sum += Number(this.id);
})

デモ

これは、「関連する」入力がチェックボックスの横にあることを前提としています。これは、デモに使用したサンプル レイアウトです。

<input type="checkbox" class="someclass" checked></input><input id="1"></input>
<input type="checkbox" class="someclass"></input><input id="2"></input>
<input type="checkbox" class="someclass"></input><input id="3"></input>
<input type="checkbox" class="someclass" checked></input><input id="7"></input>​
于 2012-09-19T00:41:54.033 に答える