How to disable other checkbox on click of one check box?

前端 未结 4 550
栀梦
栀梦 2020-12-10 21:14

I have a group of check boxes with same name, what I need is when I click any one of them, other checkboxes must get disabled. how should I apply Javascript over it?

相关标签:
4条回答
  • 2020-12-10 21:28

    Try code like this

     <script>
                function uncheck(){
                    for(var ii=1; ii<=4; ii++){
                        if(document.getElementById("q6_"+ii).checked==true){
                           document.getElementById("q6_"+ii).checked=false;
                        }
                    }
                }
            </script>
    
    0 讨论(0)
  • 2020-12-10 21:31
    <script type="text/javascript">
    for (i=0; i<document.test.finallevelusers.length; i++){
    if (document.test.finallevelusers[i].checked !=true)
      document.test.finallevelusers[i].disabled='true';
    }
    </script>
    

    probably you want them enabled again when user uncheck the checkbox

    for (i=0; i<document.test.finallevelusers.length; i++){
        if (document.test.finallevelusers[i].disabled ==true)
          document.test.finallevelusers[i].disabled='false';
        }
    
    0 讨论(0)
  • 2020-12-10 21:31

    You could do

    $('input').attr('disabled',true);
    

    ...if you really need it. But you might be better off using radio buttons.

    Try the demo

    0 讨论(0)
  • 2020-12-10 21:43
     <script type="text/javascript">
        function disableHandler (form, inputName) {
        var inputs = form.elements[inputName];
        for (var i = 0; i < inputs.length; i++) {
        var input = inputs[i];
        input.onclick = function (evt) {
        if (this.checked) {
        disableInputs(this, inputs);
        }
        else {
        enableInputs(this, inputs);
        }
        return true;
        };
        }
        }
    
        function disableInputs (input, inputs) {
        for (var i = 0; i < inputs.length; i++) {
        var currentInput = inputs[i];
        if (currentInput != input) {
        currentInput.disabled = true;
        }
        }
        }
    
        function enableInputs (input, inputs) {
        for (var i = 0; i < inputs.length; i++) {
        var currentInput = inputs[i];
        if (currentInput != input) {
        currentInput.disabled = false;
        }
        }
        }
        </script>
        </head>
        <body>
        <form name="aForm" action="">
        <p>
        <label>
        <input type="checkbox" name="finallevelusers[]" value="1">
        </label>
        <label>
    
        <input type="checkbox" name="finallevelusers[]" value="1">
        </label>
        <label>
    
        <input type="checkbox" name="finallevelusers[]" value="1">
        </label>
        </p>
        </form>
        <script type="text/javascript">
        disableHandler(document.forms.aForm, 'finallevelusers[]');
        </script>
    
    0 讨论(0)
提交回复
热议问题