admin管理员组文章数量:1277909
I have a list of dynamically created checkboxes named "tags" which I loop through with javascript to make sure only 1 is checked and to get the value. If for some reason this list of checkboxes only has 1 item, my script won't look at it. When I alert the tag's length I get undefined.
var total=[];
for (var i=0; i < document.form1.tags.length; i++){
if(document.form1.tags[i].checked==true){
total.push(document.form1.tags[i].value);
}
}
if(total.length==0){
alert("Please select a product to edit.");
}else if (total.length>1){
alert("Please select only one product to edit.");
}else{
document.location = "go somewhere";
}
I have a list of dynamically created checkboxes named "tags" which I loop through with javascript to make sure only 1 is checked and to get the value. If for some reason this list of checkboxes only has 1 item, my script won't look at it. When I alert the tag's length I get undefined.
var total=[];
for (var i=0; i < document.form1.tags.length; i++){
if(document.form1.tags[i].checked==true){
total.push(document.form1.tags[i].value);
}
}
if(total.length==0){
alert("Please select a product to edit.");
}else if (total.length>1){
alert("Please select only one product to edit.");
}else{
document.location = "go somewhere";
}
Share
Improve this question
asked Nov 8, 2012 at 22:12
KenroKenro
331 gold badge1 silver badge3 bronze badges
2
-
2
.tags
is not an array when there is only one checkbox, it contains direct reference to the one checkbox. It only bees a nodelist when there are more than one checkboxes with the same name. You should always look at the developer console and you would have seen the error there. – Esailija Commented Nov 8, 2012 at 22:14 -
@Esailija Yet another reason to use
querySelectorAll
or jQuery. – John Dvorak Commented Nov 8, 2012 at 22:16
4 Answers
Reset to default 4Why do you need Checkboxes? Wouldn't this be a great opportunity to use Radiobuttons?
Two suggestions:
use
getElementsByName
instead to get all the elements so you can iterate them:Use a framework like jquery. Any reason you can't use jquery or another framework? Your life would be so much easier.
var tags = document.getElementsByName('tags');
for(var i = 0; i < tags.length; ++i)
{
if(tags[i].checked) .....
}
You want radio buttons.
<input type='radio' name='tags' value='[whatever]' />
You won't have to 'check and make sure only one is selected'. And to get the
var tags = document.getElementsByName('tags'),
value = '', i = 0;
for( ; i < tags.length; i++ )
{
if( tags[i].checked ) {
value = tags[i].value;
break;
}
}
from the top of my head... if there is only one checkbox selected it might be document.form1.tags is not an array...
本文标签: javascriptLooping through a checkbox array that has one elementStack Overflow
版权声明:本文标题:javascript - Looping through a checkbox array that has one element - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741271652a2369408.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论