admin管理员组文章数量:1026989
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.
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
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:
<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 />';
});
/
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.
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
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:
<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/
Share Improve this question edited Mar 17, 2011 at 5:40 Rick asked Mar 17, 2011 at 2:25 RickRick 17k35 gold badges113 silver badges163 bronze badges5 Answers
Reset to default 22Try 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.
Live Demo
var elements = [];
$("div[id^='tagRow_']").each(function(){
elements.push(this.id);
});
var stringOfElementIDs = elements.toString();
$("div[id^=tagRow_]").each()....
example:
$("div[id^='tagRow_']").each(function() {
alert(this.id);
});
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.
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.
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
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.
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
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:
<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 />';
});
/
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.
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
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:
<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/
Share Improve this question edited Mar 17, 2011 at 5:40 Rick asked Mar 17, 2011 at 2:25 RickRick 17k35 gold badges113 silver badges163 bronze badges5 Answers
Reset to default 22Try 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.
Live Demo
var elements = [];
$("div[id^='tagRow_']").each(function(){
elements.push(this.id);
});
var stringOfElementIDs = elements.toString();
$("div[id^=tagRow_]").each()....
example:
$("div[id^='tagRow_']").each(function() {
alert(this.id);
});
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.
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.
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
本文标签:
版权声明:本文标题:javascript - Jquery, getting back list of ID strings of all elements with ID's that "begin with" a str 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1737596747a1482189.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论