javascript Jquery,取回所有元素的 ID 字符串列表,ID 以字符串开头?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5334186/
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-25 16:51:06  来源:igfitidea点击:

Jquery, getting back list of ID strings of all elements with ID's that "begin with" a string?

javascriptjqueryelement

提问by Rick

I'm not a Javascript guru as I do more server-side work so am struggling with this. I have found bits and pieces of how to do this. Basically, I have a series of elements that have ids beginning with the string "tagRow_" and I need to get back a list of all of the actual element ids as I need not just the element but also the element's id as I need to parse the unique ending for each on the server-side to determine what it corresponds to.

我不是 Javascript 大师,因为我做了更多的服务器端工作,所以我正在为此苦苦挣扎。我已经找到了如何做到这一点的点点滴滴。基本上,我有一系列元素的 id 以字符串“tagRow_”开头,我需要取回所有实际元素 id 的列表,因为我不仅需要元素,还需要元素的 id,因为我需要解析服务器端每个的唯一结尾,以确定它对应的内容。

I found the code below to get all elements but am not sure what it is returning as far as a list or what, if anyone can offer advice how to return just a list of string id names I would appreciate it. Thanks

我找到了下面的代码来获取所有元素,但我不确定它返回的是什么列表或什么,如果有人能提供建议如何只返回一个字符串 id 名称列表,我将不胜感激。谢谢

EDIT: I actually need to do this with a radio input field, it was by mistake I put a DIV in my own example. It works fine for a DIV but is not working properly for a radio input like below:

编辑:我实际上需要使用无线电输入字段来执行此操作,我在自己的示例中错误地放置了 DIV。它适用于 DIV,但不适用于如下所示的无线电输入:

 <input id="tagRow_ddd" type="radio" value="h">
                <input id="tagRow_ddd" type="radio" value="p">
                    <input id="tagRow_ddd" type="radio" value="r">
                    <input id="tagRow_ddd" type="radio" value="c">

$("input[id^='tagRow_']").each(function() {

   var id = this.id,
       idNumber = id.replace(/\D+/, '');
   document.body.innerHTML += idNumber + '<br />';
});

http://jsfiddle.net/fD7fP/3/

http://jsfiddle.net/fD7fP/3/

回答by Loktar

Live Demo

现场演示

var elements = [];

$("div[id^='tagRow_']").each(function(){
   elements.push(this.id); 
});

var stringOfElementIDs = elements.toString();

回答by GregL

Try the following:

请尝试以下操作:

var ids = $("div[id^='tagRow_']").map(function() {
    return this.id;
}).get();

This returns an array of all the IDs. More information from this post: Use jQuery to extract data from HTML lists and tables.

这将返回所有 ID 的数组。来自这篇文章的更多信息:使用 jQuery 从 HTML 列表和表格中提取数据

回答by Mike Lewis

$("div[id^=tagRow_]").each()....

example:

例子:

$("div[id^='tagRow_']").each(function() {    
    alert(this.id);
});

回答by alex

You can get the number on the client side too.

您也可以在客户端获取号码。

$("div[id^='tagRow_']").each(function() {

   var id = this.id,
       idNumber = id.replace(/\D+/, '');

   // idNumber contains the digits that were in the id

});

jsFiddle.

js小提琴

回答by Haochi

That will return a list of matching DOM elements (as jQuery objects). So you will have to loop through it and put the ids in a separate array.

这将返回匹配 DOM 元素的列表(作为 jQuery 对象)。所以你必须遍历它并将 id 放在一个单独的数组中。

var matching_els = $("div[id^='tagRow_']"), ids = [];
for(var i=0; i<matching_els.length; i++){
  ids.push(matching_els[i].attr("id");
}
// send ids back to server