admin管理员组文章数量:1356736
var inputs = document.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++)
{
if (document.getElementById("value_[0-9]"))
{
alert('found: ' + inputs[i].value);
}
else
{
alert(inputs[i].value);
}
}
I'm using the condition to get only input IDs that starts with value_
if (document.getElementById("value_[0-9]"))
but nothing is true where I have 5 or more IDs in my form. i.e. value_1, value_2, value_3 etc..
var inputs = document.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++)
{
if (document.getElementById("value_[0-9]"))
{
alert('found: ' + inputs[i].value);
}
else
{
alert(inputs[i].value);
}
}
I'm using the condition to get only input IDs that starts with value_
if (document.getElementById("value_[0-9]"))
but nothing is true where I have 5 or more IDs in my form. i.e. value_1, value_2, value_3 etc..
Share Improve this question edited Mar 3, 2023 at 22:47 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Feb 14, 2013 at 13:52 RafRaf 7083 gold badges13 silver badges35 bronze badges3 Answers
Reset to default 5You use wrong method for that. Simply check the id
property of a certain element in the list:
for (var i = 0, len = inputs.length; i < len; i++) {
if (inputs[i].id.indexOf("value_") === 0) {
// alert("Found: " + inputs[i].value);
}
}
Combine document.querySelectorAll
with Array.prototype.slice
to make it an Array
Array.prototype.slice.call(document.querySelectorAll('input[id^="value_"]'));
From here you can do your favorite loop, e.g. Array.prototype.forEach
[].map.call(document.getElementsByTagName( 'input' ), function( input ) {
return input.id.indexOf( 'value_' ) === 0;
}).forEach(function( input ) {
// do whatever here
});
本文标签: javascriptLoop through input elements and get all ids that starts with 39value39Stack Overflow
版权声明:本文标题:javascript - Loop through input elements and get all ids that starts with 'value_' - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744039911a2580471.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论