开发者

Jquery selector to generate total where checkbox is ticked

I am trying to create a dynamic total generator, which adds a value to the total when the relative checkbox in the is checked, but I am struggling with the selector logic. Can anyone point me as to where I am going wrong, I am a beginner with JQuery. Here is what I have

function calculateSelected(){
    var sum = 0;
    $('tr input:checked .price').each(function() {
            sum += parseFloat($(this).text());
        });
    $('#total').html(sum)
}

And this is a simplified version of the table

<table>
<tr>
<td><input type="checkbox" value='1' /><td>
<td class='price'>100</td>
</tr>
<tr>
<td><input type="checkbox" value='1' /><td>
<td class='price'>100</td>
</tr>
<tr>
<td><input type="checkbox" value='1' /><td>
<td class='price'>100</td>
</tr>
<tr>
<td><input type="checkbox" value=开发者_JAVA百科'1' /><td>
<td class='price'>100</td>
</tr>
<tr>
<td>Total<td>
<td id='total'></td>
</tr>
</table>


What is this selecting exactly?

$('tr input:checked .price')

How is there a child of your input? If you want to select the next td with class price you can do:

$('tr input:checked').parent().next('td.price')


Much like Mark's answer you could use the Siblings method too.

$('tr input:checked').parent().siblings('td.price')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜