Javascript 如何在单击一个复选框时禁用其他复选框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4426482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 12:21:59  来源:igfitidea点击:

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

javascriptjavascript-eventscheckbox

提问by OM The Eternity

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?

我有一组同名的复选框,我需要的是当我单击其中任何一个时,其他复选框必须被禁用。我应该如何在它上面应用 javascript?

<input type="checkbox" name="finallevelusers[]" value="1"/>
<input type="checkbox" name="finallevelusers[]" value="1"/>
<input type="checkbox" name="finallevelusers[]" value="1"/>
<input type="checkbox" name="finallevelusers[]" value="1"/>

Please help...

请帮忙...

采纳答案by Shakti Singh

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

回答by bozdoz

You could do

你可以做

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

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

……如果你真的需要它。但是您最好使用单选按钮。

Try the demo

尝试演示

回答by Bhanu Prakash Pandey

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

回答by Symfony

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>