检索当前行的表行索引 [英] Retrieve Table Row Index of Current Row

查看:119
本文介绍了检索当前行的表行索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在失去焦点时验证文本输入。我想知道它所在的表格中的哪一行。这是我到目前为止所持有的并且它一直以未定义的形式返回。有什么想法?

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();
    }
});


推荐答案

rowIndex 是一个DOM属性,而不是jQuery方法,所以你必须在底层DOM对象上调用它:

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

tableRow[0].rowIndex

或只是:

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

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

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

在jQuery 1.4中有 $(row).index(),但它会扫描兄弟姐妹以找出它在父节点中的子元素编号。如果你有多个< tbody> s,这会慢一些,并会返回不同的结果 rowIndex

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 rowIndex in the case where you have multiple <tbody>​s.

这篇关于检索当前行的表行索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆