我在html页面上有一个带有大量复选框作为选项的表单。选项之一是“全部选中”,我希望所有其他复选框都被选中(如果未选中),则选中“全部选中”框。我的代码如下所示:
<form method = "post" class = "notification-options">
<input type = "checkbox" name = "notification-option" id = "all-post" onClick = "javascript:checkALL(this
);"> All Posts <br/>
<input type = "checkbox" name = "notification-option" id = "others-post"> Other's Posts <br/>
<input type = "checkbox" name = "notification-option" id = "client-post"> Cilent's Post <br/>
<input type = "checkbox" name = "notification-option" id = "assign-post"> Task Assigned </form>
Java脚本:
<script type = "text/javascript">
var $check-all = document.getElementbyId("all-post");
function checkALL($check-all){
if ($check-all.checked == true){
document.getElementByName("notification-option").checked = true;
}
}
</script>
当我运行代码时,什么都没有发生
这是一些准则。
type
属性不是必需的,可以省略。getElementById()
this
从在线处理程序传递数据的同时,您正在使用全局变量名称作为参数。传递的参数在函数中隐藏了全局变量。if (checkAll.checked)
做这份工作getElementsByName()
,gEBN()
返回的HTMLCollection,这是阵列状物体。您必须遍历集合,并分别设置checked
每个元素。固定代码:
<script>
var checkAll = document.getElementById("all-post");
function checkALL(){
var n, checkboxes;
if (checkAll.checked){
checkboxes = document.getElementsByName("notification-option");
for (n = 0; n < checkboxes.length; n++) {
checkboxes[n].checked = true;
}
}
}
</script>
您还可以javascript:
从在线处理程序中省略伪协议和参数。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句