简体   繁体   English

查找包含文本JQuery的表行索引

[英]Find table row index that contains text JQuery

I am trying to find table row index from a link which is on the table. 我试图从表上的链接中查找表行索引。 The code I am trying, always returning value 3. I have two many rows on my page. 我正在尝试的代码始终返回值3。我的页面上有两行。 My code is - 我的代码是-

Table (example) - 表(示例)-

<table>
  <tbody>
   <tr>
    <td>
     <div>
       <a herf="my link here" onclick="findindex(Rank1)">Rank1</a>
     </div>
    </td>
   </tr>
  </tbody>
</table>

JQuery - jQuery-

function findindex(RName) {
    var val = $('table tbody tr td div a:contains(' + RName + ')').index();
    alert(val);
});

Please someone help me how to find the table row index value. 请有人帮助我如何找到表行索引值。

Thanks 谢谢

When you call index() on that selector, you're in fact getting the index of the a element. 当在该选择器上调用index()时,实际上就是在获取a元素的索引。 The text selector is working fine, but after finding the element with the mached text, you need to find it's tr and get it's index: 文本选择器工作正常,但是在找到包含文本的元素之后,您需要找到它的tr并获取它的索引:

$('table tbody tr td div a:contains("' + RName + '")').closest("tr").index();

Demo 演示版

Also, you have some syntax errors: 另外,您还有一些语法错误:

onclick="findindex(Rank1)" 

Should be: 应该:

onclick="findindex('Rank1')" 

Or even better: 甚至更好:

onclick="findindex(this)" 

And the function: 和功能:

function findindex(element) {
    var val = $('table tbody tr td div a:contains("' + element.innerText + '")').closest("tr").index();
    alert(element.innerText + "=" + val);
};

Demo 演示版

Note that I have added quotes to your css selector: 请注意,我已经在您的CSS选择器中添加了引号:

'table tbody tr td div a:contains("' + element.innerText + '")'
                                  ^ this                    ^ and this

To produce this result 'table tbody tr td div a:contains("Rank1")' 要产生此结果,请使用'table tbody tr td div a:contains("Rank1")'


Now, the best thing you can do with jQuery: 现在,您可以使用jQuery做的最好的事情:

Remove the onclick event attribute from your anchor elements and add a class: 从锚元素中删除onclick事件属性,并添加一个类:

<a herf="my link here" class="rank-anchor">Rank1</a>

Then bind an event on document load to your table: 然后将文档加载时的事件绑定到表:

$("table").on("click", "a.rank-anchor", function() {
    var index = $(this).closest("tr").index();
    alert(index);
});

In the event function, you can see that you don't need the full selector to the a element, you already have the element, so you can use this to find its tr . 在事件函数中,您可以看到不需要a元素的完整选择器,因为您已经有了该元素,因此可以使用this来找到其tr In this case you don't need to find the element by it's content as well, the event is being called fron it already. 在这种情况下,您也不需要通过元素的内容来查找元素,因为该事件已被调用。

Demo 演示版

HTML : HTML:

<table>
  <tbody>
   <tr>
    <td>
     <div>
       <a herf="my link here" onclick="findindex(this)">Rank1</a>
     </div>
    </td>
   </tr>
   <tr>
    <td>
     <div>
       <a herf="my link here" onclick="findindex(this)">Rank2</a>
     </div>
    </td>
   </tr><tr>
    <td>
     <div>
       <a herf="my link here" onclick="findindex(this)">Rank3</a>
     </div>
    </td>
   </tr><tr>
    <td>
     <div>
       <a herf="my link here" onclick="findindex(this)">Rank4</a>
     </div>
    </td>
   </tr><tr>
    <td>
     <div>
       <a herf="my link here" onclick="findindex(this)">Rank5</a>
     </div>
    </td>
   </tr><tr>
    <td>
     <div>
       <a herf="my link here" onclick="findindex(this)">Rank6</a>
     </div>
    </td>
   </tr><tr>
    <td>
     <div>
       <a herf="my link here" onclick="findindex(this)">Rank6</a>
     </div>
    </td>
   </tr>
  </tbody>
</table>

JavaScript Function : JavaScript函数:

function findindex(obj) {
    var val = $(obj).closest("tr").index();
    alert("Rank = " + val);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM