javascript jquery 如何检查表中是否有具有特定 id 的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13829644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:53:39  来源:igfitidea点击:

How to check if a table has a row with a specific id jquery

javascriptjqueryhtmljsonasp.net-mvc-3

提问by Carl Weis

How do I check if a table contains a row with a specific id using jQuery?

如何使用 jQuery 检查表是否包含具有特定 id 的行?

回答by Sushanth --

Just

只是

if($('#rowID').length){

  }

should be sufficient..

应该够了。。

ID'son a page are unique.. So this check should do..

页面上的ID是唯一的.. 所以这个检查应该做..

回答by Blazemonger

if ($('table#uniqueID').find('#rowID').length > 0) {

回答by I Hate Lazy

Once you have the table, it's easier and faster without jQuery.

一旦你有了表格,没有 jQuery,它会更容易和更快。

var table = $('table')[0];
if (table.rows["theId"])
    alert("Has the row!");

DEMO:http://jsfiddle.net/wUKyk/

演示:http ://jsfiddle.net/wUKyk/

回答by adeneo

if ($("#elementID").length) { ... }

ID's are unique, so just checking if the ID exists with lengthis enough

ID 是唯一的,所以只要检查 ID 是否存在length就足够了

回答by elclanrs

var hasRowX = $table.find('#rowX').length;

回答by Marnix.hoh

To extend on the answer given by 'I Hate Lazy', this also works if the row in question has a 'name' attribute instead of an 'id'. I.e.:

为了扩展“我讨厌懒惰”给出的答案,如果相关行具有“名称”属性而不是“id”,这也适用。IE:

var table = $('table')[0];
if (table.rows["theName"])
    alert("Has the row!");