簡體   English   中英

keyup上的文本字段的總和會導致行的第一個字段中出現問題

[英]Summation of text fields on keyup causes issues in first field of a row

我有一張桌子。我試圖找出如下總和: td(1) + td(2) + td(3) = td(4)td(5) + td(6) + td(7) = td(8)td(9) + td(10) + td(11) = td(12)

這是我的代碼:

$(document).ready(function () {
    $('#table').on('keyup', 'input', function () {
        $("#table tr").slice(2).find("td:nth-child(4n + 1)").each(function () {
            var sum = 0;
            $(this).prevAll(':lt(3)').find('input').each(function () {
                sum += (+this.value || 0)
            });
            $(this).find('input').val(sum)
        })
    })
})

上面的代碼工作正常。 但我的問題是,我不能輸入任何東西到第一列(即td:eq(0) )。 我的代碼有什么問題?

http://jsfiddle.net/b0svwpnn/3/

您需要明確地排除第nth-child選擇的nth-child一個input ,您可以使用:not()來實現。 嘗試這個:

$("#table tr").slice(2).find("td:nth-child(4n + 1):not(:first)")

更新了小提琴

暫無
暫無

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

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