我试图使用jquery检查/取消选中所有复选框。现在通过选中/取消选中父复选框,所有子复选框都被选中/取消选中,父复选框的文本也变为checkall / uncheckall。
现在我想用输入按钮替换父复选框,并更改按钮上的文本以checkall / uncheckall。这个代码,任何人都可以调整代码....
$( function() {
$( '.checkAll' ).live( 'change', function() {
$( '.cb-element' ).attr( 'checked', $( this ).is( ':checked' ) ? 'checked' : '' );
$( this ).next().text( $( this ).is( ':checked' ) ? 'Uncheck All' : 'Check All' );
});
$( '.cb-element' ).live( 'change', function() {
$( '.cb-element' ).length == $( '.cb-element:checked' ).length ? $( '.checkAll' ).attr( 'checked', 'checked' ).next().text( 'Uncheck All' ) : $( '.checkAll' ).attr( 'checked', '' ).next().text( 'Check All' );
});
});
<input type="checkbox" class="checkAll" /> <b>Check All</b>
<input type="checkbox" class="cb-element" /> Checkbox 1
<input type="checkbox" class="cb-element" /> Checkbox 2
<input type="checkbox" class="cb-element" /> Checkbox 3
试试这个:
$(document).ready(function(){
$('.check:button').toggle(function(){
$('input:checkbox').attr('checked','checked');
$(this).val('uncheck all');
},function(){
$('input:checkbox').removeAttr('checked');
$(this).val('check all');
})
})
你可以试试这个
$('.checkAll').on('click',function(){
$('.checkboxes').prop('checked',$(this).prop("checked"));
});`
类.checkAll
是一个控制批量操作的复选框
试试这个
$(".checkAll").click(function() {
if("checkall" === $(this).val()) {
$(".cb-element").attr('checked', true);
$(this).val("uncheckall"); //change button text
}
else if("uncheckall" === $(this).val()) {
$(".cb-element").attr('checked', false);
$(this).val("checkall"); //change button text
}
});
无耻的自我推销:there's a jQuery plugin for that。
HTML:
<form action="#" id="myform">
<div><input type="checkbox" id="checkall"> <label for="checkall"> Check all</label></div>
<fieldset id="slaves">
<div><label><input type="checkbox"> Checkbox</label></div>
<div><label><input type="checkbox"> Checkbox</label></div>
<div><label><input type="checkbox"> Checkbox</label></div>
<div><label><input type="checkbox"> Checkbox</label></div>
<div><label><input type="checkbox"> Checkbox</label></div>
</fieldset>
</form>
JS:
$('#checkall').checkAll('#slaves input:checkbox', {
reportTo: function () {
var prefix = this.prop('checked') ? 'un' : '';
this.next().text(prefix + 'check all');
}
});
......你已经完成了。
试试这个,
<input type="checkbox" class="checkAll" onclick="$('input[type=checkbox][class=cb-element]').attr('checked',this.checked)">
您可以使用this.checked
来验证复选框的当前状态,
$('.checkAll').change(function(){
var state = this.checked; //checked ? - true else false
state ? $(':checkbox').prop('checked',true) : $(':checkbox').prop('checked',false);
//change text
state ? $(this).next('b').text('Uncheck All') : $(this).next('b').text('Check All')
});
$('.checkAll').change(function(){
var state = this.checked;
state? $(':checkbox').prop('checked',true):$(':checkbox').prop('checked',false);
state? $(this).next('b').text('Uncheck All') :$(this).next('b').text('Check All')
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" class="checkAll" /> <b>Check All</b>
<input type="checkbox" class="cb-element" /> Checkbox 1
<input type="checkbox" class="cb-element" /> Checkbox 2
<input type="checkbox" class="cb-element" /> Checkbox 3
尝试以下代码来检查/取消选中所有复选框
jQuery(document).ready(function() {
$("#check_uncheck").change(function() {
if ($("#check_uncheck:checked").length) {
$(".checkboxes input:checkbox").prop("checked", true);
} else {
$(".checkboxes input:checkbox").prop("checked", false);
}
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="checkbox" name="check_uncheck" id="check_uncheck" /> Check All/Uncheck All
<br/>
<br/>
<div class="checkboxes">
<input type="checkbox" name="check" id="check" /> Check Box 1
<br/>
<input type="checkbox" name="check" id="check" /> Check Box 2
<br/>
<input type="checkbox" name="check" id="check" /> Check Box 3
<br/>
<input type="checkbox" name="check" id="check" /> Check Box 4
</div>
试试这个Demo Link
还有另一种简短的方法,例如以下结帐。在此示例中,选中是否选中所有复选框,如果选中,则选中所有复选框,然后检查所有复选框,如果不是全部将取消选中
$("#check_uncheck").change(function() {
$(".checkboxes input:checkbox").prop("checked",$(this).is(':checked'));
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="checkbox" name="check_uncheck" id="check_uncheck" /> Check All/Uncheck All
<br/>
<br/>
<div class="checkboxes">
<input type="checkbox" name="check" id="check" /> Check Box 1
<br/>
<input type="checkbox" name="check" id="check" /> Check Box 2
<br/>
<input type="checkbox" name="check" id="check" /> Check Box 3
<br/>
<input type="checkbox" name="check" id="check" /> Check Box 4
</div>
使用jQuery检查/取消选中All in Intermediate属性
使用getSelectedItems()方法获取Array中的Checked Items
来源Checkbox List Select / Unselect All with Indeterminate Master Check
HTML
<div class="container">
<div class="card">
<div class="card-header">
<ul class="list-group list-group-flush">
<li class="list-group-item">
<input
class="form-check-input"
type="checkbox"
value="selectAll"
id="masterCheck"
/>
<label class="form-check-label" for="masterCheck">
Select / Unselect All
</label>
</li>
</ul>
</div>
<div class="card-body">
<ul class="list-group list-group-flush" id="list-wrapper">
<li class="list-group-item">
<input
class="form-check-input"
type="checkbox"
value="item1"
id="item1"
/>
<label class="form-check-label" for="item1">
Item 1
</label>
</li>
<li class="list-group-item">
<input
class="form-check-input"
type="checkbox"
value="item2"
id="item2"
/>
<label class="form-check-label" for="item2">
Item 2
</label>
</li>
<li class="list-group-item">
<input
class="form-check-input"
type="checkbox"
value="item3"
id="item3"
/>
<label class="form-check-label" for="item3">
Item 3
</label>
</li>
<li class="list-group-item">
<input
class="form-check-input"
type="checkbox"
value="item4"
id="item4"
/>
<label class="form-check-label" for="item4">
Item 4
</label>
</li>
<li class="list-group-item">
<input
class="form-check-input"
type="checkbox"
value="item5"
id="item5"
/>
<label class="form-check-label" for="item5">
Item 5
</label>
</li>
<li class="list-group-item" id="selected-values"></li>
</ul>
</div>
</div>
</div>
jQuery的
$(function() {
// ID selector on Master Checkbox
var masterCheck = $("#masterCheck");
// ID selector on Items Container
var listCheckItems = $("#list-wrapper :checkbox");
// Click Event on Master Check
masterCheck.on("click", function() {
var isMasterChecked = $(this).is(":checked");
listCheckItems.prop("checked", isMasterChecked);
getSelectedItems();
});
// Change Event on each item checkbox
listCheckItems.on("change", function() {
// Total Checkboxes in list
var totalItems = listCheckItems.length;
// Total Checked Checkboxes in list
var checkedItems = listCheckItems.filter(":checked").length;
//If all are checked
if (totalItems == checkedItems) {
masterCheck.prop("indeterminate", false);
masterCheck.prop("checked", true);
}
// Not all but only some are checked
else if (checkedItems > 0 && checkedItems < totalItems) {
masterCheck.prop("indeterminate", true);
}
//If none is checked
else {
masterCheck.prop("indeterminate", false);
masterCheck.prop("checked", false);
}
getSelectedItems();
});
function getSelectedItems() {
var getCheckedValues = [];
getCheckedValues = [];
listCheckItems.filter(":checked").each(function() {
getCheckedValues.push($(this).val());
});
$("#selected-values").html(JSON.stringify(getCheckedValues));
}
});
我知道这个问题已经过时但我注意到大多数人都在使用复选框。接受的答案使用按钮,但无法使用多个按钮(例如,底部第一页顶部的一个按钮)。所以这是两个都做的修改。
HTML
<a href="#" class="check-box-machine my-button-style">Check All</a>
jQuery的
var ischecked = false;
$(".check-box-machine").click(function(e) {
e.preventDefault();
if (ischecked == false) {
$("input:checkbox").attr("checked","checked");
$(".check-box-machine").html("Uncheck All");
ischecked = true;
} else {
$("input:checkbox").removeAttr("checked");
$(".check-box-machine").html("Check All");
ischecked = false;
}
});
这将允许您在更改文本和复选框值时拥有任意数量的按钮。我包括了一个e.preventDefault()
调用,因为这将阻止页面跳转到顶部由于href="#"
部分。
$(function () {
$('input#check_all').change(function () {
$("input[name='input_ids[]']").prop('checked', $(this).prop("checked"));
});
});
这是一个链接,用于选中和取消选中父复选框,所有子复选框都被选中和取消选中。
jquery check uncheck all checkboxes Using Jquery With Demo
$(function () {
$("#select-all").on("click", function () {
var all = $(this);
$('input:checkbox').each(function () {
$(this).prop("checked", all.prop("checked"));
});
});
});
这是我发现的最短路径(需要jQuery1.6 +)
HTML:
<input type="checkbox" id="checkAll"/>
JS:
$("#checkAll").change(function () {
$("input:checkbox").prop('checked', $(this).prop("checked"));
});
我正在使用.prop as .attr不适用于jQuery 1.6+中的复选框,除非您已明确将checked属性添加到输入标记中。
例-
$("#checkAll").change(function () {
$("input:checkbox").prop('checked', $(this).prop("checked"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form action="#">
<p><label><input type="checkbox" id="checkAll"/> Check all</label></p>
<fieldset>
<legend>Loads of checkboxes</legend>
<p><label><input type="checkbox" /> Option 1</label></p>
<p><label><input type="checkbox" /> Option 2</label></p>
<p><label><input type="checkbox" /> Option 3</label></p>
<p><label><input type="checkbox" /> Option 4</label></p>
</fieldset>
</form>
<script type="text/javascript">
function myFunction(checked,total_boxes){
for ( i=0 ; i < total_boxes ; i++ ){
if (checked){
document.forms[0].checkBox[i].checked=true;
}else{
document.forms[0].checkBox[i].checked=false;
}
}
}
</script>
<body>
<FORM>
<input type="checkbox" name="checkBox" >one<br>
<input type="checkbox" name="checkBox" >two<br>
<input type="checkbox" name="checkBox" >three<br>
<input type="checkbox" name="checkBox" >four<br>
<input type="checkbox" name="checkBox" >five<br>
<input type="checkbox" name="checkBox" >six<br>
<input type="checkbox" name="checkBox" >seven<br>
<input type="checkbox" name="checkBox" >eight<br>
<input type="checkbox" name="checkBox" >nine<br>
<input type="checkbox" name="checkBox" >ten<br> s
<input type=button name="CheckAll" value="Select_All" onClick="myFunction(true,10)">
<input type=button name="UnCheckAll" value="UnCheck All Boxes" onClick="myFunction(false,10)">
</FORM>
</body>
将其添加到您的代码块中,甚至单击您的按钮
$('input:checkbox').attr('checked',false);
$(document).ready( function() {
// Check All
$('.checkall').click(function () {
$(":checkbox").attr("checked", true);
});
// Uncheck All
$('.uncheckall').click(function () {
$(":checkbox").attr("checked", false);
});
});
如果未检查所有项目,请选中全部取消选中/检查控制器
JS:
e =复选框id t =复选框(item)class n =复选框检查所有类
function checkAll(e,t,n){jQuery("#"+e).click(function(e){if(this.checked){jQuery("."+t).each(function(){this.checked=true;jQuery("."+n).each(function(){this.checked=true})})}else{jQuery("."+t).each(function(){this.checked=false;jQuery("."+n).each(function(){this.checked=false})})}});jQuery("."+t).click(function(e){var r=jQuery("."+t).length;var i=0;var s=0;jQuery("."+t).each(function(){if(this.checked==true){i++}if(this.checked==false){s++}});if(r==i){jQuery("."+n).each(function(){this.checked=true})}if(i<r){jQuery("."+n).each(function(){this.checked=false})}})}
HTML:
检查所有控制类:chkall_ctrl
<input type="checkbox"name="chkall" id="chkall_up" class="chkall_ctrl"/>
<br/>
1.<input type="checkbox" value="1" class="chkall" name="chk[]" id="chk1"/><br/>
2.<input type="checkbox" value="2" class="chkall" name="chk[]" id="chk2"/><br/>
3.<input type="checkbox" value="3" class="chkall" name="chk[]" id="chk3"/><br/>
<br/>
<input type="checkbox"name="chkall" id="chkall_down" class="chkall_ctrl"/>
<script>
jQuery(document).ready(function($)
{
checkAll('chkall_up','chkall','chkall_ctrl');
checkAll('chkall_down','chkall','chkall_ctrl');
});
</script>
试试这个:
HTML
<input type="checkbox" name="all" id="checkall" />
JavaScript的
$('#checkall:checkbox').change(function () {
if($(this).attr("checked")) $('input:checkbox').attr('checked','checked');
else $('input:checkbox').removeAttr('checked');
});
HTML
<input type="checkbox" name="select_all" id="select_all" class="checkAll" />
使用Javascript
$('.checkAll').click(function(){
if($(this).attr('checked')){
$('input:checkbox').attr('checked',true);
}
else{
$('input:checkbox').attr('checked',false);
}
});
试试这个:
$('.checkAll').on('click', function(e) {
$('.cb-element').prop('checked', $(e.target).prop('checked'));
});
e
是你执行click
时生成的事件,而e.target
是点击的元素(.checkAll
),所以你只需要将类checked
的属性.cb-element
与类的.checkAll`一样。
PS:请原谅我糟糕的英语!
使用按钮切换复选框的解决方案,与jQuery 1.9+兼容,其中toogle-event is no longer available:
$('.check:button').click(function(){
var checked = !$(this).data('checked');
$('input:checkbox').prop('checked', checked);
$(this).val(checked ? 'uncheck all' : 'check all' )
$(this).data('checked', checked);
});
最佳解决方案Check Fiddle
$("#checkAll").change(function () {
$("input:checkbox.cb-element").prop('checked', $(this).prop("checked"));
});
$(".cb-element").change(function () {
_tot = $(".cb-element").length
_tot_checked = $(".cb-element:checked").length;
if(_tot != _tot_checked){
$("#checkAll").prop('checked',false);
}
});
<input type="checkbox" id="checkAll"/> ALL
<br />
<input type="checkbox" class="cb-element" /> Checkbox 1
<input type="checkbox" class="cb-element" /> Checkbox 2
<input type="checkbox" class="cb-element" /> Checkbox 3
如果用户选中所有复选框,则会检查所有复选框,并且如果用户选中任何一个复选框,则检查所有复选框将取消选中。
$("#checkall").change(function () {
$("input:checkbox").prop('checked', $(this).prop("checked"));
});
$(".cb-element").change(function () {
if($(".cb-element").length==$(".cb-element:checked").length)
$("#checkall").prop('checked', true);
else
$("#checkall").prop('checked', false);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="checkbox" name="all" id="checkall" />Check All</br>
<input type="checkbox" class="cb-element" /> Checkbox 1</br>
<input type="checkbox" class="cb-element" /> Checkbox 2</br>
<input type="checkbox" class="cb-element" /> Checkbox 3
同意Richard Garside的简短回答,但不要在prop()
中使用$(this).prop("checked")
,你可以使用checked
的原生JS checkbox属性,
$("#checkAll").change(function () {
$("input:checkbox").prop('checked', this.checked);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form action="#">
<p><label><input type="checkbox" id="checkAll"/> Check all</label></p>
<fieldset>
<legend>Loads of checkboxes</legend>
<p><label><input type="checkbox" /> Option 1</label></p>
<p><label><input type="checkbox" /> Option 2</label></p>
<p><label><input type="checkbox" /> Option 3</label></p>
<p><label><input type="checkbox" /> Option 4</label></p>
</fieldset>
</form>