我正试图在类似于该表的表中的特定行之后附加一个字符串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:查找具有特定数据属性值的行(的索引)
发布评论