使用Javascript和放大器; ASP.NET - 管理的CheckBoxList [英] Javascript & ASP.NET - Manage CheckBoxList
问题描述
我有一个的CheckBoxList dinamically从DB填写。
I have a CheckBoxList dinamically filled from DB.
<asp:CheckBoxList ID="chklist1" runat="server" onclick="chklist1_onclick()" />
奥西已经填满我有几个选择,其中之一有文无回应。
Oce it has been filled I have several options and one of them has the text "No Response".
我要的是,做下面的JavaScript函数:
What I want is a javascript function that does the following:
1)如果我检查没有响应所有其它选项必须取消选中。
1) If I check "No Response" all other options must be unchecked.
2)如果我检查是不是无应答的选项至少有一个,无应答选项必须选中。
2) If I check at least one of the options that is not "No Response", the "No Response" option must be unchecked.
希望予以明确。先谢谢了。
Hope to be clear. Thanks in advance.
我的尝试是:
function chklist1_onclick() {
var chklist1 = document.getElementById('<%= chklist1.ClientID %>');
var chkList = chklist1.getElementsByTagName("input");
for (var i = 0; i < chkList.length; i++) {
if (chkList[i].checked && chkList[i].value == "6") {
for (var i = 0; i < chkList.length; i++) {
if (chkList[i].checked && chkList[i].value != "6") {
chkList[i].checked = false;
}
}
}
}
}
其中6是无响应项的值。不过这样一来,我只解决案件1)
Where 6 is the value of "No Response" item. But this way I only resolve the case 1)
推荐答案
假设无响应
复选框具有值=
你可以尝试下面的脚本:
Assuming that the No Response
checkbox has value=""
you could try the following script:
<script type="text/javascript">
window.onload = function () {
var noResponseCheckBoxFilter = function(item) {
return item.value == '';
};
var otherCheckboxesFilter = function(item) {
return !noResponseCheckBoxFilter(item);
};
var childInputs = document.getElementById('<%= chklist1.ClientID %>').getElementsByTagName('input');
var checkboxes = Array.prototype.slice.call(childInputs, 0).filter(function (item) {
return item.type == 'checkbox';
});
for (var i = 0; i < checkboxes.length; i++) {
checkboxes[i].onclick = function () {
if (this.value == '') {
if (this.checked) {
// uncheck the other checkboxes
var otherCheckboxes = checkboxes.filter(otherCheckboxesFilter);
for (var j = 0; j < otherCheckboxes.length; j++) {
otherCheckboxes[j].checked = false;
}
}
} else {
// uncheck the No Response checkbox
checkboxes.filter(noResponseCheckBoxFilter)[0].checked = false;
}
};
}
};
</script>
如果您没有响应复选框具有不同的值比空字符串简单地适应在previous例如测试。
If your No Response checkbox has a different value than the empty string simply adapt the tests in the previous example.
这篇关于使用Javascript和放大器; ASP.NET - 管理的CheckBoxList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!