Javascript 检索当前行的表行索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2545603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:49:54  来源:igfitidea点击:

Retrieve Table Row Index of Current Row

javascriptjqueryvalidation

提问by Jon

I am trying to validate a text input when it loses focus. I would like to know which row of the table it is in. This is what I have so far and it keeps coming back as undefined. Any ideas?

我试图在失去焦点时验证文本输入。我想知道它在表的哪一行。这是我到目前为止所拥有的,它一直以未定义状态返回。有任何想法吗?

$("div#step-2 fieldset table tbody tr td input").blur(function() {
    var tableRow = $(this).parent().parent();
    if ($.trim($(this).val()) == "") {
        $(this).addClass("invalid");
        alert(tableRow.rowIndex);
        $(this).val("");
    } else {
        $(this).removeClass("invalid");
        checkTextChanges();
    }
});

回答by bobince

rowIndexis a DOM property, not a jQuery method, so you have to call it on the underlying DOM object:

rowIndex是 DOM 属性,而不是 jQuery 方法,因此您必须在底层 DOM 对象上调用它:

tableRow[0].rowIndex

or just:

要不就:

var row= this.parentNode.parentNode;
alert(row.rowIndex);

since you aren't really using jQuery for much there.

因为你在那里并没有真正使用 jQuery。

In jQuery 1.4 there is $(row).index(), but it scans siblings to find out which child element number it is in its parent. This is slower and will return a different result to rowIndexin the case where you have multiple <tbody>?s.

在 jQuery 1.4 中有$(row).index(),但它会扫描兄弟元素以找出它在其父元素中的子元素编号。这会比较慢,并且rowIndex在您有多个<tbody>?s的情况下会返回不同的结果。

回答by PetersenDidIt

With jQuery 1.4.* you can use the index() method.

在 jQuery 1.4.* 中,您可以使用index() 方法。

Your selector is a little more specific then it needs to be. Also you should use the closestmethod rather then multiple parent() calls. Also cache $(this).

您的选择器比它需要的更具体一些。此外,您应该使用最接近的方法而不是多个 parent() 调用。还缓存 $(this)。

$("#step-2 fieldset table td input").blur(function() {
    var that = $(this),
        tableRow = that.closest('tr');
    if ($.trim(that.val()) == "") {
        that.addClass("invalid");
        alert(tableRow.index());
        that.val("");
    } else {
        that.removeClass("invalid");
        checkTextChanges();
    }
});

Also alert is not a very good debugging tool, might be time for you to check out firebug

警报也不是一个很好的调试工具,可能是时候检查一下firebug了

回答by janmoesen

You are trying to use the DOM Core attribute on the jQuery object. Try this:

您正在尝试在 jQuery 对象上使用 DOM Core 属性。尝试这个:

alert(tableRow[0].rowIndex);

alert(tableRow[0].rowIndex);

@jandreas: from the W3C docs: rowIndex of type long, readonly, modified in DOM Level 2This is in logical order and not in document order. The rowIndex does take into account sections (THEAD, TFOOT, or TBODY) within the table, placing THEAD rows first in the index, followed by TBODY rows, followed by TFOOT rows.

@jandreas:来自 W3C 文档: rowIndex of type long, readonly, modified in DOM Level 2这是按逻辑顺序排列的,而不是按文档顺序排列的。rowIndex 确实考虑了表中的部分(THEAD、TFOOT 或 TBODY),将 THEAD 行放在索引中,然后是 TBODY 行,然后是 TFOOT 行。

.index()would not take those THEAD etc. into account.

.index()不会考虑那些 THEAD 等。

回答by easement

try closest('tr')if you are version 1.3+ . it'll work better than parent().parent()

如果您是 1.3+ 版本,请尝试最接近 ('tr')。它会比 parent().parent() 更好用