[英]<FORM> If no checkbox is selected, alert! .. ELSE .. submit. (Javascript)
语言: Javascript
我真的很讨厌问这样一个看似简单的问题,但看起来很简单,我无法让它发挥作用。
我试图完全使用纯Javascript(没有库支持) 。
我有一个带复选框的表格......
所有复选框都命名为files[]
因为我在数组中使用结果:
<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>
我想要做的是,当用户提交表单时:
这是我的表格 :
<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>
这是我的Javascript代码 :
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;
}
}
在行动: http : //jsfiddle.net/DVqwB/3/
显然,它不起作用,我知道我应该使用getElementsById
但由于它们每个都有唯一的ID我不能使用它。 而且我也知道这个网站上有很多解决方案,但如果你看 - 他们实际上使用jQuery ...
任何帮助和指导将不胜感激! 非常感谢。
迭代集合的正确方法是:(完整示例)
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
返回NodeList(类似于数组),而不是单个元素。
您必须循环遍历它并依次测试每个元素,并且只有在到达循环结束时才处理“else”方案而不查找条件匹配。
你可以使用这个脚本。只需在表格的行动中改变你的文件名
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.