簡體   English   中英

jQuery選擇器生成總的復選框

[英]Jquery selector to generate total where checkbox is ticked

我正在嘗試創建一個動態的總計生成器,當選中中的相對復選框時,該值會在總計中添加一個值,但是我在選擇器邏輯上苦苦掙扎。 任何人都可以指出我出了問題的地方,我是JQuery的初學者。 這是我所擁有的

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

這是表格的簡化版本

<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='1' /><td>
<td class='price'>100</td>
</tr>
<tr>
<td>Total<td>
<td id='total'></td>
</tr>
</table>

這到底是什么選擇?

$('tr input:checked .price')

您輸入的內容如何? 如果您要選擇下一個含班級價格的td,可以執行以下操作:

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

就像Mark的答案一樣,您也可以使用Siblings方法

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM