javascript 复选框列表检查功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6297733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
checkboxlist check functionality
提问by chamara
I have a CheckBoxList id="cblFiles"
and a CheckBox id="Checkbox1"
.
我有一个CheckBoxList id="cblFiles"
和一个CheckBox id="Checkbox1"
。
I have added checkAll functionality to the CheckBoxList. And now what I need to check is if any CheckBox in the CheckBoxList is unchecked "Checkbox1" should be unchecked.
我已将 checkAll 功能添加到 CheckBoxList。现在我需要检查的是 CheckBoxList 中的任何 CheckBox 是否未选中“Checkbox1”应该取消选中。
Following code doesn't work for me
以下代码对我不起作用
function SelectNoneCheckboxes() {
var elm = document.getElementById("<%=cblFiles.ClientID %>");
for (i = 0; i < elm.childNodes.length; i++) {
if (elm.childNodes[i].checked == false) {
document.getElementById("<%=Checkbox1.ClientID %>").checked = false;
}
}
}
code for the checkAll Functionality
checkAll 功能的代码
function SelectAllCheckboxes(spanChk) {
var oItem = spanChk.children;
var theBox = (spanChk.type == "checkbox") ?
spanChk : spanChk.children.item[0];
xState = theBox.checked;
elm = theBox.form.elements;
for (i = 0; i < elm.length; i++)
if (elm[i].type == "checkbox" &&
elm[i].id != theBox.id) {
if (elm[i].checked != xState)
elm[i].click();
}
}
回答by Yuriy Rozhovetskiy
Try this
试试这个
function SelectNoneCheckboxes() {
var elm = document.getElementById("<%=cblFiles.ClientID %>");
var checkBoxes = elm.getElementsByTagName("input");
for (i = 0; i < checkBoxes.length; i++) {
if (checkBoxes[i].checked == false) {
document.getElementById("<%=Checkbox1.ClientID %>").checked = false;
}
}
}
Server code
服务器代码
protected void Page_PreRender(object sender, EventArgs e)
{
CheckBox1.Attributes["onClick"] = "CheckAll(this.checked);";
}
Script
脚本
function CheckAll(value) {
var checkBoxList = document.getElementById("<%= CheckBoxList1.ClientID %>");
if (value !== true) return;
var checkBoxes = checkBoxList.getElementsByTagName("input");
for (i = 0; i < checkBoxes.length; i++) {
checkBoxes[i].checked = true;
}
}
回答by chamara
function SelectNoneCheckboxes() {
var elm = document.getElementById("<%=cblFiles.ClientID %>");
var checkBoxes = elm.getElementsByTagName("input");
for (i = 0; i < checkBoxes.length; i++) {
if (checkBoxes[i].checked == false) {
document.getElementById("<%=Checkbox1.ClientID %>").checked = false;
break;
}
else {
document.getElementById("<%=Checkbox1.ClientID %>").checked = true;
}
}
}