[英]How to disable all checkbox with use of foreach in jQuery
I want to disable all checkbox when click all 'select all'. 单击全部“全选”时,我想禁用所有复选框。
How is this possible using jQuery? 使用jQuery怎么可能?
JavaScript: JavaScript的:
Here categories[]
is name of checkbox which is in foreach loop 这里
categories[]
是foreach循环中复选框的名称
function checkAll(source) {
checkboxes = document.getElementsByName('categories[]');
for(var i=0, n=checkboxes.length;i<n;i++) {
//checkboxes[i].checked = source.checked;
checkboxes[i].disabled = source.disabled;
}
}
Just use prop('disabled', true)
只需使用
prop('disabled', true)
$('#mainChk').on('click', function(){ var categories = $('.categories'); categories.prop('disabled', !categories.prop('disabled')); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="mainChk" type="checkbox" > Main <input type="checkbox" class="categories"> First <input type="checkbox" class="categories"> Second <input type="checkbox" class="categories"> Third
Give all your checkbox a class (ex: class='ck'). 给您所有的复选框一个类(例如:class ='ck')。
Then : 然后 :
$('.ck').each(function(){
$(this).prop('disabled', true);
})
function uncheckAll() { $('input:checkbox').removeAttr('checked'); } function disableAll() { $('input:checkbox').attr('disabled','true'); }
<input type="checkbox" checked>A<br/> <input type="checkbox" checked>B<br/> <input type="checkbox">C<br/> <input type="checkbox" checked>D<br/> <input type="checkbox">E<br/> <button onclick="uncheckAll()">Uncheck all</button> <button onclick="disableAll()">Disable all</button> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
This will work for you. 这将为您工作。
<input type="checkbox" checked>A<br/>
<input type="checkbox" checked>B<br/>
<input type="checkbox">C<br/>
<input type="checkbox" checked>D<br/>
<input type="checkbox">E<br/>
<button class="disableAll">Disable all</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".disableAll").click(function(){
$('input[type=checkbox]').each(function(){
$(this).prop('disabled', true);
})
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.