[英]Disable inputs by class when checkbox selected
So I have dynamically created checkboxes that share the same class: 因此,我动态创建了共享同一类的复选框:
<input type="checkbox" class="boxes" />
<input type="checkbox" class="boxes" />
And have a few dropdown options that share that same class also: 并具有一些与该类共享相同的下拉选项:
<select class = "needToDisable">
<option value="volvo">Volvo</option>
</select>
As these are generated dynamically, I went for this approach to check if the length of the checked checkboxes is > 0. If so disable all dropdowns with the class "needToDisable" 由于这些是动态生成的,因此我采用了这种方法来检查选中的复选框的长度是否>0。如果是这样,请使用“ needToDisable”类禁用所有下拉菜单
$(".boxes").on('click',function(){
if ($('.boxes:checked').length > 0) {
$(".boxes").prop("disabled", true);
} else {
$(".boxes").prop("disabled", false);
}
});
I have seen a few other examples like: 我还看到了其他一些示例,例如:
Using jquery to get all checked checkboxes with a certain class name 使用jQuery获取具有特定类名称的所有选中的复选框
But it says "click" is not defined, I don't think I can use in this way. 但是它说“点击”没有定义,我认为我不能以这种方式使用。
First, you need to use 'click'
and not click
. 首先,您需要使用'click'
而不是click
。 Secondly, your select class is needToDisable
, which needs to be disabled and not .boxes
. 其次,您的选择类是needToDisable
,需要将其禁用而不是.boxes
。 Below is a small working demo: 下面是一个小型的工作演示:
$(".boxes").on('click', function() { $(".needToDisable").prop("disabled", $('.boxes:checked').length); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="checkbox" class="boxes" /> <input type="checkbox" class="boxes" /> <select class="needToDisable"> <option value="volvo">Volvo</option> </select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.