jQuery:查找具有特定数据属性值的行(的索引) [英] JQuery: Find (the index of) a row with a specific data attribute value

查看:128
本文介绍了jQuery:查找具有特定数据属性值的行(的索引)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图在类似于该表的表中的特定行之后附加一个字符串myoutput:

I am trying to append a string, myoutput, right after a specific row in a table similar to this one:

<table>
<tr data-my_id='1'> <td> content </td> </tr>
<tr data-my_id='2' data-my_other_id='1' > <td> content </td> </tr>
<tr data-my_id='3' data-my_other_id='2' > <td> content </td> </tr>
</table>

所以说我想用data-my_other_id='2'在tr之后附加我的输出字符串 (请注意,在我的代码中,my_other_id = 2已经)

So let's say I want to append my output string after the tr with data-my_other_id='2' (note that in my code, my_other_id = 2 already )

我正在努力做到这一点:

I am trying to accomplish it doing this:

var want = $("tr").find("[data-my_other_id='" + my_other_id + "']").index();

找到索引后,我想将输出内容附加到此行...

after finding the index, I want to append my output strhing to this row...

$(want).insertAfter(...?);

也...每当我注意时,

Also... I noticed whenever I do

alert( want = $("tr").find("[data-my_other_id='" + my_other_id + "']").length)

我得到0 ...

请帮忙,如果我的问题不够清楚,请告诉我,以便我更好地解释.

Help please and let me know if my question is not clear enough so I can explain better.

推荐答案

我假设您要更新内容而不是附加内容,但是它并没有真正改变任何内容.我不认为您想以这种方式使用find().尝试类似的东西:

I'm assuming you want to update the content rather than append, but it doesn't really change anything. I don't think you want to use find() that way. Try something like:

var $row = $('tr[data-my_other_id="' + id + '"]');
// should be the index of the tr in the <table>
// this may not be a good idea though - what if you add a header row later?
var index = row.index() + 1; // if you want 1-based indices
$row.find("td").text("I am row #" + index);

这篇关于jQuery:查找具有特定数据属性值的行(的索引)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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