简体   繁体   English

取消选中一个复选框后如何启用所有复选框? 使用JavaScript

[英]How to enable all checkbox when one checkbox is deselected ? using javascript

My Code works when I select one checkbox , all gets disabled. 当我选中一个复选框时,所有代码都被禁用,我的代码可以正常工作。 Now i want that if i uncheck this selected checkbox, all checkbox should get enabled. 现在,我希望如果我取消选中此选中的复选框,则所有复选框都应启用。 Here is what i have tried 这是我尝试过的

<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: This javascript is when one selects the a checkbox , all gets disabled. JS:这个JavaScript是当选中一个复选框时,全部被禁用。 I want vice versa, that when one deselects the checkbox all should get enabled 反之亦然,当我取消选中复选框时,所有复选框均应启用

function validate(){

            for (i=0; i<document.myForm.check1.length; i++){
                if (document.myForm.check1[i].checked !=true){
                    document.myForm.check1[i].disabled='true';
                }
            }
 }
<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>

Try this 尝试这个

In the For loop's First if condition checkes whether each element is checked , It disables the elements which are not checked. 在For循环的First if条件中检查是否已检查每个元素,它将禁用未检查的元素。

In the second If condition, it checkes whether the clicked element is not checked and enables all elements.. 在第二个If条件中,它检查是否未选中clicked元素并启用所有元素。

and in the end it enables the clicked element. 最后,它启用了clicked元素。

Pretty Confusing but serves the purpose... 相当令人困惑,但达到了目的...

Several things to be mentioned: 需要提及的几件事:

  • Once you ckeck the result it is better to leave that single checkbox enabled 确认结果后,最好保留该复选框为启用状态
  • You don't need to add onclick to all checkboxes - it is not efficient, hard to maintain and will produce behind the scenes anonymous functions for each such declaration; 您不需要将onclick添加到所有复选框-它效率不高,难以维护,并且会为每个这样的声明在后台生成匿名函数; You may attach event to parent of all checkboxes and just check the target of the event to filter out all unrelevant elements. 您可以将事件附加到所有复选框的父级,然后仅检查事件的目标即可过滤掉所有不相关的元素。

Here is my solution for you: 这是我为您提供的解决方案:

HTML 的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 的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
    }
  }
});

Here is a working example: http://jsbin.com/iKiNenA/10/edit 这是一个工作示例: http : //jsbin.com/iKiNenA/10/edit

Note: You should place the <script>...</script> after the <form>...</form> in order to be sure that it runs after form DOM is loaded. 注意:应该将<script>...</script>放在<form>...</form> ,以确保它在加载表单DOM后运行。 Otherwise wrap the code with document.onload = funciton() { ... } 否则,将代码包装为document.onload = funciton() { ... }

With jQuery you can reduce your javascript code just to this: 使用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
    }
  });

});

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

Tested on IE8+, FF, Safari, Chrome (on Mac and Windows) 在IE8 +,FF,Safari,Chrome(在Mac和Windows上)上进行了测试

If you insist to use javascript instead of jQuery as this could be very simple in jQuery, please check here : 如果您坚持使用JavaScript而不是jQuery,因为这在jQuery中可能非常简单,请检查此处:

http://jsfiddle.net/CYLmn/18/ 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='';
            }
        }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当取消选择任何一个孩子时,如何取消选中全选复选框 - How to uncheck select all checkbox when any one of their child got deselected 如何使用javascript启用带有复选框的所有单选按钮? - How to enable all the radio button with a checkbox by using javascript? 当使用JavaScript取消选中复选框时,如何启用文本框 - how to enable textbox ,when uncheck a checkbox using javascript 如何使用JavaScript在GridView中启用和禁用CheckBox? - How to enable and disable CheckBox in the GridView using JavaScript? 仅使用 javascript 设置一个复选框即可启用所有其他复选框 - make one checkbox enable all others purely with javascript 在Angular 4中取消选中复选框时,如何从JSON数组中删除对象? - How to remove an object from JSON array when checkbox is deselected in Angular 4? 如何使用复选框启用 Javascript 代码? - How to enable Javascript code with a checkbox? 使用JavaScript的一个复选框禁用选择框并启用文本框 - Disable select box and enable textbox using one checkbox with JavaScript 如果使用 javascript 选中至少一个复选框,则启用按钮 - Enable a button if atleast one checkbox is checked using javascript 选中和取消选中复选框时切换文本 - Toggle text when the checkbox is selected and deselected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM