[英]How to disable checkbox with jquery?
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);
});
id
should be unique. id
应该是唯一的。 You cannot have four checkboxes with the same id. 您不能有四个具有相同ID的复选框。
You can try other selectors to select the whole range of checkboxes, like .checkbox1
(by class), input[type="checkbox"]
(by tag/attribute). 您可以尝试其他选择器来选择整个范围的复选框,例如
.checkbox1
(按类), input[type="checkbox"]
(按标签/属性)。 Once you've fixed the ids, you could even try #chk1, #chk2, #chk3, #chk4
. 一旦你修复了id,你甚至可以尝试
#chk1, #chk2, #chk3, #chk4
。
The snippet below uses the classname 'chk' instead of the id 'chk'. 下面的代码段使用了类名'chk'而不是id'chk'。 Also, it uses
attr
to set the attribute although it did work for me using prop
as well. 此外,它使用
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
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之间更改属性。
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);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.