我有4个复选框列表,现在我想要的是,当一个已被选中时,其他复选框将被禁用。
我现在试图找几个小时的答案,但我无法找到解决问题的方法。
这是我的代码:
<input id="check1" type="checkbox" name="checkbox1" value="1" onclick="call(this)"/>
<input id="check2" type="checkbox" name="checkbox2" value="1" onclick="call(this)"/>
<input id="check3" type="checkbox" name="checkbox3" value="1" onclick="call(this)"/>
<input id="check4" type="checkbox" name="checkbox4" value="1" onclick="call(this)"/>
</body>
答案 0 :(得分:0)
使用此HTML代码:
<form id="foo">
<input id="check1" type="radio" name="rad" value="1" onclick="call(this)"/>
<input id="check2" type="radio" name="rad" value="1" onclick="call(this)"/>
<input id="check3" type="radio" name="rad" value="1" onclick="call(this)"/>
<input id="check4" type="radio" name="rad" value="1" onclick="call(this)"/>
</form>
这个javascript代码:
function call(obj){
nCheckBoxes = 4;
//unchecks all the checkboxes
for(i=1;i<=nCheckBoxes;i++){
document.getElementById("check"+i).checked = false;
}
obj.checked = true;
//other functions here
}
答案 1 :(得分:0)
我正在使用手机,但如果您使用的是jQuery,这样的话应该会有效:
$('input[type="checkbox"]').on('change', function() {
$('input[type="checkbox"]').prop('checked', false);
$(this).prop('checked', true);
});
答案 2 :(得分:0)
这完全符合您的需要:
function call(obj) {
var temp = obj.checked
i = 1;
for (; i <= 4; i++) {
document.getElementById("check" + i).checked = false;
document.getElementById("check" + i).disabled = temp ? "disabled" : "";
}
obj.checked = temp;
obj.disabled = "";
}
这是有效的jsFiddle