如果取消选中一个复选框,如何启用所有复选框?使用javascript

时间:2013-11-29 07:10:04

标签: javascript

当我选择一个复选框时,我的代码有效,所有都被禁用。现在我希望如果我取消选中此选中的复选框,则应启用所有复选框。 这是我试过的

<tr> <td colspan='3'> <input type='checkbox' name='check1' value = '80' onclick='return(validate())'>Q- 80. Statute of Liberty is in ? </td> </tr>
<tr> <td colspan='3'> <input type='checkbox' name='check1' value = '153' onclick='return(validate())'>Q- 153. Indias Republic day is celebrated on ? </td> </tr>
<tr> <td colspan='3'> <input type='checkbox' name='check1' value = '65' onclick='return(validate())'>Q- 65. Popular Language is ? </td> </tr>
<tr> <td colspan='3'> <input type='checkbox' name='check1' value = '150' onclick='return(validate())'>Q- 150. Girnar is located at ? </td> </tr>
<tr> <td colspan='3'> <input type='checkbox' name='check1' value = '154' onclick='return(validate())'>Q- 154. Largest Waterfall ? </td> </tr>
<tr> <td colspan='3'> <input type='checkbox' name='check1' value = '152' onclick='return(validate())'>Q- 152. Asia is an ? </td> </tr>
<tr> <td colspan='3'> <input type='checkbox' name='check1' value = '139' onclick='return(validate())'>Q- 139. Diamond is made up of ? </td> </tr>
<tr> <td colspan='3'> <input type='checkbox' name='check1' value = '123' onclick='return(validate())'>Q- 123. Largest Desert ? </td> </tr>

JS:这个javascript是当选择一个复选框时,所有都被禁用。我反之亦然,当取消选中复选框时,所有应该启用

function validate(){

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

3 个答案:

答案 0 :(得分:0)

<script>
    function validate(element){
        for (i=0; i<document.myForm.check1.length; i++){
            if (!(document.myForm.check1[i].checked)){
                document.myForm.check1[i].disabled=true;
            }
            if(!(element.checked)){
                document.myForm.check1[i].disabled=false;
            }
            element.disabled=false;
        }
    }
</script>
<form name="myForm" id="myForm">
<tr> <td colspan='3'> <input type='checkbox' name='check1' value = '80' onclick='return(validate(this))'>Q- 80. Statute of Liberty is in ? </td> </tr>
<tr> <td colspan='3'> <input type='checkbox' name='check1' value = '153' onclick='return(validate(this))'>Q- 153. Indias Republic day is celebrated on ? </td> </tr>
<tr> <td colspan='3'> <input type='checkbox' name='check1' value = '65' onclick='return(validate(this))'>Q- 65. Popular Language is ? </td> </tr>
<tr> <td colspan='3'> <input type='checkbox' name='check1' value = '150' onclick='return(validate(this))'>Q- 150. Girnar is located at ? </td> </tr>
<tr> <td colspan='3'> <input type='checkbox' name='check1' value = '154' onclick='return(validate(this))'>Q- 154. Largest Waterfall ? </td> </tr>
<tr> <td colspan='3'> <input type='checkbox' name='check1' value = '152' onclick='return(validate(this))'>Q- 152. Asia is an ? </td> </tr>
<tr> <td colspan='3'> <input type='checkbox' name='check1' value = '139' onclick='return(validate(this))'>Q- 139. Diamond is made up of ? </td> </tr>
<tr> <td colspan='3'> <input type='checkbox' name='check1' value = '123' onclick='return(validate(this))'>Q- 123. Largest Desert ? </td> </tr>
</form>

试试这个

在For循环的First if中,检查是否检查了每个元素,它会禁用未检查的元素。

在第二个If条件中,它检查是否未选中被点击的元素并启用所有元素..

并最终启用点击的元素。

相当令人困惑,但却有目的......

答案 1 :(得分:0)

有几件事需要提及:

  • 关闭结果后,最好不要启用该单一复选框
  • 您不需要在所有复选框中添加onclick - 它效率低,难以维护,并且会为每个此类声明生成幕后匿名函数;您可以将事件附加到所有复选框的父级,只需检查事件的目标以过滤掉所有不相关的元素。

以下是我的解决方案:

<强> HTML

<form id="myForm">
  <table>
    <tr> <td colspan='3'> <input type='checkbox' name='check1' value = '80'>Q- 80. Statute of Liberty is in ? </td> </tr>
    <tr> <td colspan='3'> <input type='checkbox' name='check1' value = '153'>Q- 153. Indias Republic day is celebrated on ? </td> </tr>
    <tr> <td colspan='3'> <input type='checkbox' name='check1' value = '65'>Q- 65. Popular Language is ? </td> </tr>
    <tr> <td colspan='3'> <input type='checkbox' name='check1' value = '150'>Q- 150. Girnar is located at ? </td> </tr>
    <tr> <td colspan='3'> <input type='checkbox' name='check1' value = '154'>Q- 154. Largest Waterfall ? </td> </tr>
    <tr> <td colspan='3'> <input type='checkbox' name='check1' value = '152'>Q- 152. Asia is an ? </td> </tr>
    <tr> <td colspan='3'> <input type='checkbox' name='check1' value = '139'>Q- 139. Diamond is made up of ? </td> </tr>
    <tr> <td colspan='3'> <input type='checkbox' name='check1' value = '123'>Q- 123. Largest Desert ? </td> </tr>
  </table>
</form>

<强>的JavaScript

function listen(element, event, callback) {
  if(element.attachEvent) { // IE
    element.attachEvent('on'+event, callback);
  } else { // W3C
    element.addEventListener(event, callback);
  }
}

var form = document.querySelector('#myForm');
listen(form, 'click', function(event){
  var checkBoxes, i, checked, target;
  target = event.srcElement || event.target; // IE or W3C 
  if(target.getAttribute('name') === 'check1') {
    checkBoxes = form.querySelectorAll('input[name="check1"]');
    checked = target.checked;
    for(i=0;i<checkBoxes.length;i++) {
      checkBoxes[i].disabled = checked && checkBoxes[i] !== target;
    }
    if(checked) {
      // Validate and do something with result (target.value)
    } else {
      // Reset validation and result
    }
  }
});

以下是一个工作示例:http://jsbin.com/iKiNenA/10/edit

注意:您应该将<script>...</script>放在<form>...</form>之后,以确保它在加载表单DOM后运行。否则用document.onload = funciton() { ... }

包装代码

使用jQuery,你可以减少你的javascript代码:

$(function() {

  $('#myForm').on('click', 'input[name="check1"]', function(event) {
    $('#myForm input[name="check1"]').prop('disabled', event.target.checked);
    event.target.disabled = false;
    if(event.target.checked) {
      // Validate and do something with result (event.target.value)
    } else {
      // Reset validation and result
    }
  });

});

示例:http://jsbin.com/iKiNenA/9/edit

在IE8 +,FF,Safari,Chrome(Mac和Windows)上测试

答案 2 :(得分:0)

如果你坚持使用javascript而不是jQuery,因为这在jQuery中非常简单,请点击这里:

http://jsfiddle.net/CYLmn/18/

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

            for (i=0; i<document.myForm.check1.length; i++){
                document.myForm.check1[i].disabled='';
            }
        }
 }