< FORM>如果没有选择复选框,提醒! ..提交。 (Javascript) [英] <FORM> If no checkbox is selected, alert! .. ELSE .. submit. (Javascript)
问题描述
语言: Javascript
我真的很讨厌提出这样一个看似简单的问题,但看起来很简单,这个工作。
我试图完全用单独的JavaScript(没有库支持)。
我有一个带有复选框的表单...
所有复选框都命名为 files []
,因为我使用结果数组:
< input type ='checkbox' name ='file []'id ='1'value ='1'/>文件1< br>
< input type ='checkbox'name ='files []'id ='2'value ='2'/>文件2< br>
< input type ='checkbox'name ='files []'id ='3'value ='3'/>文件3< br>
我想要做的是,当用户提交 如果未选中复选框,请返回 ALERT!。 li>
/ strong>:
< form name =deleteFiles action =method =postonsubmit =return confirm_update();>
< input type ='checkbox'name ='files []'id ='1'value ='1'/>文件1< br>
< input type ='checkbox'name ='files []'id ='2'value ='2'/>文件2< br>
< input type ='checkbox'name ='files []'id ='3'value ='3'/>文件3< br>
< input type =submitvalue =Submitname =submit>
< / form>
代码::
function confirm_update
var aCheckbox = document.deleteFiles.getElementsByTagName('input');
if(aCheckbox.checked){
return confirm(您确定要继续删除所选文件吗?
} else {
alert(您没有要删除的任何选定文件。
return false;
}
}
在操作中: http://jsfiddle.net/DVqwB/3/
显然,它不工作,我知道我应该使用 getElementsById
,但因为他们都有唯一的ID,我不能使用。我也知道在这个网站有很多解决方案,但如果你看 - 他们实际上使用jQuery ...
任何帮助&指导将非常感谢!非常感谢。
迭代集合的正确方法是:(full example)
function confirm_update(){var arrCheckboxes = document.deleteFiles.elements [files []]; var checkCount = 0; for(var i = 0; i
body {margin:30px;}
< form name =deleteFilesaction =method = postonsubmit =return confirm_update();> < input type ='checkbox'name ='files []'id ='1'value ='1'/>文件1< br /> < input type ='checkbox'name ='files []'id ='2'value ='2'/>文件2< br /> < input type ='checkbox'name ='files []'id ='3'value ='3'/>文件3< br /> < input type =submitvalue =Submitname =submit/> < / form>
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.net/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.
Correct 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>
这篇关于< FORM>如果没有选择复选框,提醒! ..提交。 (Javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!