admin管理员组文章数量:1405516
Language: Javascript
I really hate to ask such a seemingly simple question, but simple as it seems, I can't get this to work.
I am trying to do this entirely with pure Javascript alone (no library support).
I have a form with checkboxes...
All checkboxes are named files[]
because I use the results in an array:
<input type='checkbox' name='files[]' id='1' value='1' /> file 1<br>
<input type='checkbox' name='files[]' id='2' value='2' /> file 2<br>
<input type='checkbox' name='files[]' id='3' value='3' /> file 3<br>
What I'm trying to do is, when the user submits the form:
- IF no checkbox is checked >> return ALERT!
- ELSE submit the form
Here's my form:
<form name="deleteFiles" action="" method="post" onsubmit="return confirm_update();">
<input type='checkbox' name='files[]' id='1' value='1' /> file 1<br>
<input type='checkbox' name='files[]' id='2' value='2' /> file 2<br>
<input type='checkbox' name='files[]' id='3' value='3' /> file 3<br>
<input type="submit" value="Submit" name="submit">
</form>
And here's my Javascript code:
function confirm_update() {
var aCheckbox = document.deleteFiles.getElementsByTagName('input');
if (aCheckbox.checked){
return confirm("Are you sure you want to proceed deleting the selected files?");
} else {
alert("You do not have any selected files to delete.");
return false;
}
}
In action: /
Apparently, it is not working, I know I should use getElementsById
but since they each have unique IDs I can't use that. And I also know that there are lots of solutions on this site, but if you look - they actually use jQuery...
Any help & guidance would be greatly appreciated! Thank you so much.
Language: Javascript
I really hate to ask such a seemingly simple question, but simple as it seems, I can't get this to work.
I am trying to do this entirely with pure Javascript alone (no library support).
I have a form with checkboxes...
All checkboxes are named files[]
because I use the results in an array:
<input type='checkbox' name='files[]' id='1' value='1' /> file 1<br>
<input type='checkbox' name='files[]' id='2' value='2' /> file 2<br>
<input type='checkbox' name='files[]' id='3' value='3' /> file 3<br>
What I'm trying to do is, when the user submits the form:
- IF no checkbox is checked >> return ALERT!
- ELSE submit the form
Here's my form:
<form name="deleteFiles" action="" method="post" onsubmit="return confirm_update();">
<input type='checkbox' name='files[]' id='1' value='1' /> file 1<br>
<input type='checkbox' name='files[]' id='2' value='2' /> file 2<br>
<input type='checkbox' name='files[]' id='3' value='3' /> file 3<br>
<input type="submit" value="Submit" name="submit">
</form>
And here's my Javascript code:
function confirm_update() {
var aCheckbox = document.deleteFiles.getElementsByTagName('input');
if (aCheckbox.checked){
return confirm("Are you sure you want to proceed deleting the selected files?");
} else {
alert("You do not have any selected files to delete.");
return false;
}
}
In action: http://jsfiddle/DVqwB/3/
Apparently, it is not working, I know I should use getElementsById
but since they each have unique IDs I can't use that. And I also know that there are lots of solutions on this site, but if you look - they actually use jQuery...
Any help & guidance would be greatly appreciated! Thank you so much.
Share asked Nov 14, 2012 at 9:53 MafiaMafia 8122 gold badges20 silver badges39 bronze badges3 Answers
Reset to default 2Correct way to iterate the collection will be: (full example)
function confirm_update() {
var arrCheckboxes = document.deleteFiles.elements["files[]"];
var checkCount = 0;
for (var i = 0; i < arrCheckboxes.length; i++) {
checkCount += (arrCheckboxes[i].checked) ? 1 : 0;
}
if (checkCount > 0){
return confirm("Are you sure you want to proceed deleting the selected files?");
} else {
alert("You do not have any selected files to delete.");
return false;
}
}
body {
margin: 30px;
}
<form name="deleteFiles" action="" method="post" onsubmit="return confirm_update();">
<input type='checkbox' name='files[]' id='1' value='1' /> file 1<br />
<input type='checkbox' name='files[]' id='2' value='2' /> file 2<br />
<input type='checkbox' name='files[]' id='3' value='3' /> file 3<br />
<input type="submit" value="Submit" name="submit" />
</form>
getElementsByTagName
returns a NodeList (which is like an array), not a single element.
You have to loop over it and test each element in turn, and only handle the "else" scenario if you get to the end of the loop without finding a match for the condition.
you can use this script.just chang your filename in action of form
<form id="deleteFiles" name="deleteFiles" action="yourFileName.html" method="post" onsubmit="return confirm_update();">
<input type='checkbox' name='files[]' id='1' value='1' /> file 1<br>
<input type='checkbox' name='files[]' id='2' value='2' /> file 2<br>
<input type='checkbox' name='files[]' id='3' value='3' /> file 3<br>
<input type="submit" value="Submit" name="submit">
</form>
<script type="text/javascript">
function confirm_update() {
check=false;
var aCheckbox = document.getElementById('deleteFiles').elements;
for(i=0;i<aCheckbox.length;++i)
{
if(aCheckbox[i].type=='checkbox' && aCheckbox[i].checked)
{
check=true;
}
}
if(check==true)
{
return confirm("Do u really want to delete?");
}
else
{alert("you haven't selected any of the checkboxex");
return false;
}
}
</script>
本文标签: ltFORMgt If no checkbox is selectedalertELSEsubmit (Javascript)Stack Overflow
版权声明:本文标题:<FORM> If no checkbox is selected, alert! .. ELSE .. submit. (Javascript) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744917414a2632078.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论