Javascript 如何使用jquery禁用复选框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30826769/
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
How to disable checkbox with jquery?
提问by Brian Crist
I searched more time with it but it's not work, I want to checkbox is disabled, user not check and can check it if some condition. Ok, now, I tried disabled them. I use jquery 2.1.3
我用它搜索了更多时间但它不起作用,我想禁用复选框,用户不检查并且可以在某些条件下检查它。好的,现在,我尝试禁用它们。我使用 jquery 2.1.3
<input type="checkbox" class="checkbox1" id="chk" name="check[]" value="U01" />Banana
<input type="checkbox" class="checkbox1" id="chk" name="check[]" value="U02" />Orange
<input type="checkbox" class="checkbox1" id="chk" name="check[]" value="U03" />Apple
<input type="checkbox" class="checkbox1" id="chk" name="check[]" value="U04" />Candy
$(window).load(function () {
$('#chk').prop('disabled', true);
});
回答by GolezTrol
idshould be unique. You cannot have four checkboxes with the same id.
id应该是独一无二的。您不能有四个具有相同 ID 的复选框。
You can try other selectors to select the whole range of checkboxes, like .checkbox1(by class), input[type="checkbox"](by tag/attribute). Once you've fixed the ids, you could even try #chk1, #chk2, #chk3, #chk4.
您可以尝试其他选择器来选择整个范围的复选框,例如.checkbox1(按类)、input[type="checkbox"](按标签/属性)。一旦你修复了 id,你甚至可以尝试#chk1, #chk2, #chk3, #chk4.
The snippet below uses the classname 'chk' instead of the id 'chk'. Also, it uses attrto set the attribute although it did work for me using propas well.
下面的代码段使用类名“chk”而不是 id“chk”。此外,它用于attr设置属性,尽管它也对我有用prop。
$(window).load(function() {
$('.chk').attr('disabled', true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" class="chk" name="check[]" value="U01" />Banana
<input type="checkbox" class="chk" name="check[]" value="U02" />Orange
<input type="checkbox" class="chk" name="check[]" value="U03" />Apple
<input type="checkbox" class="chk" name="check[]" value="U04" />Candy
回答by Yann Ptheitroadé
You already changed the ID but you can also put the class in the same attribute such as:
您已经更改了 ID,但您也可以将类放在相同的属性中,例如:
<input type="checkbox" class="checkbox1 chk" name="check[]" value="U01" />Banana
Then you can use jQuery to either disable or check a checkbox depending on you needs like so
然后您可以使用 jQuery 根据您的需要禁用或选中复选框
To disable:
要禁用:
$(function () {
$('.chk').prop('disabled', true);
});
To "precheck":
要“预检”:
$(function () {
$('.chk').prop('checked', true);
});
You can change the selector to fit IDs or classes even elements and change the properties between true or false according to your needs.
您可以更改选择器以适应 ID 或类甚至元素,并根据需要在 true 或 false 之间更改属性。
回答by sachin kumar
HTML:
HTML:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" class="checkbox1" class="chk" name="check[]" value="U01" />Banana
<input type="checkbox" class="checkbox1" class="chk" name="check[]" value="U02" />Orange
<input type="checkbox" class="checkbox1" class="chk" name="check[]" value="U03" />Apple
<input type="checkbox" class="checkbox1" class="chk" name="check[]" value="U04" />Candy
Javascript/jQuery
Javascript/jQuery
$(function() {
$("input.checkbox1").prop("disabled", true);
});

