语言: 使用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>
我正在试图做的是,当用户提交表单:
- 如果没有复选框被选中>>返回警惕!
- ELSE提交表单
这是我的表格 :
<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 ...
任何帮助和指导,将不胜感激! 非常感谢。