当选中一个复选框时,表单中的所有其他复选框都必须禁用-不工作

When one check is selected all other checkbox in a form must get disabled - Not Working

本文关键字:复选框 其他 工作 表单 一个      更新时间:2023-11-12

我有一个带有多个复选框的表单。现在,当我点击其中一个复选框时,另一个应该会被禁用。我的JavaScript代码部分不起作用,即选中了一个复选框,其他所有复选框都没有被禁用

<tr> <td> <input type='checkbox' name='check1' value = '79'>Q- 79. Currency of japan </td> </tr>
<tr> <td> <input type='checkbox' name='check1' value = '80'>Q- 80. Statute of Liberty is in ? </td> </tr>
<tr> <td> <input type='checkbox' name='check1' value = '64'>Q- 64. National Animal ? </td> </tr>
<tr> <td> <input type='checkbox' name='check1' value = '65'>Q- 65. Popular language </td> </tr>
<tr> <td> <input type='checkbox' name='check1' value = '63'>Q- 63. Largest Ocean ? </td> </tr>

JS:

function isUncheck(){
        for (i=0; i<document.myForm.check1.length; i++){
        if (document.myForm.check1[i].checked !=true)
        document.myForm.check1[i].disabled='true';
}

我的函数调用:

<tr><td> <input type="submit" name="edit" style="height: 20px;width: 250px" formmethod='post' formaction='/~xyz/cgi-bin/Project/CheckEdit.py' value="Edit" onclick="return(isUncheck())"></td></tr><br>

http://jsfiddle.net/Z4XSU/

onclick="isUncheck();"
function isUncheck(){
  var checkboxes=document.getElementsByName("check1");
   for (i=0; i<checkboxes.length; i++){
    if (!checkboxes[i].checked)   checkboxes[i].disabled='true';
   }
}

我会为此而被烧死的。在用户体验方面,你应该使用单选按钮。但是,由于您坚持使用复选框,下面是解决方案。

function updateOthers(target){
     var chk = document.getElementsByTagName('input');
     for(var i=0; i < chk.length; i++) {
        if(chk[i].type == 'checkbox') {
            if(chk[i] != target) {
                chk[i].disabled = true;
            }
        }
     }
   }