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

时间:2013-11-27 09:07:17

标签: javascript onclick

我有一个带有多个复选框的表单。现在当我点击其中一个复选框时,另一个应该被禁用。 我的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>

2 个答案:

答案 0 :(得分:1)

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';
   }
}

答案 1 :(得分:0)

我要为此而烧死地狱。 UX明智的你应该使用单选按钮。 但是,由于您坚持使用复选框here's the solution

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;
            }
        }
     }
   }