jQuery:查找具有特定数据属性值的行(的索引)

编程入门 行业动态 更新时间:2024-10-27 03:42:10
本文介绍了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:查找具有特定数据属性值的行(的索引)

本文发布于:2023-10-23 19:41:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1521821.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:索引   属性   数据   jQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!