jquery check 버튼으로 모든 확인란을 선택 취소하십시오.
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');
})
})
이것이 내가 찾은 가장 짧은 방법입니다 (jQuery1.6 이상 필요)
HTML :
<input type="checkbox" id="checkAll"/>
JS :
$("#checkAll").change(function () {
$("input:checkbox").prop('checked', $(this).prop("checked"));
});
입력 태그에 체크 된 속성을 명시 적으로 추가하지 않으면 jQuery 1.6 이상에서 .attr이 확인란에서 작동하지 않으므로 .prop를 사용하고 있습니다.
예-
$("#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>
이거 한번 해봐:
HTML
<input type="checkbox" name="all" id="checkall" />
자바 스크립트
$('#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" />
자바 스크립트
$('.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
)이므로 클래스가 .checkAll` 인 요소와 같은 checked
클래스의 요소 속성 만 입력하면됩니다 .cb-element
.
추신 : 영어 실례합니다!
toogle -event를 더 이상 사용할 수없는 jQuery 1.9+와 호환되는 버튼을 사용하여 확인란을 전환하는 솔루션 :
$('.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);
});
여기에 최고의 솔루션 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")
기본 JS checked
속성을 사용할 수 있습니다 .
$("#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>
당신은 이것을 시도 할 수 있습니다
$('.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
}
});
뻔뻔한 자체 프로모션 :이를 위한 jQuery 플러그인이 있습니다.
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');
}
});
... 끝났어요.
http://jsfiddle.net/mattball/NrM2P
이 시도,
<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>
이 데모 링크를 사용해보십시오
이 작업을 수행하는 또 다른 짧은 방법이 있습니다. 아래 예를 확인하십시오. 이 예에서 모든 체크 / 체크 해제 체크 박스가 체크되어 있거나 체크되어 있지 않으면 모든 와이어가 체크되고 모두 체크되지 않을 것입니다
$("#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를 사용하여 중간 속성으로 모두 확인 / 선택 취소
getSelectedItems () 메소드를 사용하여 배열에서 점검 항목 가져 오기
소스 체크 박스리스트 불확정 마스터 체크로 모두 선택 / 선택 해제
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"));
});
});
부모 확인란을 선택 및 선택 해제하고 모든 자식 확인란을 선택 및 선택 해제하는 데 사용되는 링크가 있습니다.
$(function () {
$("#select-all").on("click", function () {
var all = $(this);
$('input:checkbox').each(function () {
$(this).prop("checked", all.prop("checked"));
});
});
});
<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 = 확인란 (항목) 클래스 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>
참고 URL : https://stackoverflow.com/questions/5229023/jquery-check-uncheck-all-checkboxes-with-a-button
'Programming' 카테고리의 다른 글
두 날짜 사이의 근무일 계산 (0) | 2020.06.12 |
---|---|
C ++과 Java에서 "일반"유형의 차이점은 무엇입니까? (0) | 2020.06.12 |
다차원 배열의 너비와 높이를 어떻게 얻습니까? (0) | 2020.06.12 |
반응 양식의 소품 변경 상태 업데이트 (0) | 2020.06.12 |
한 Datatable에서 다른 DataTable로 행을 복사 하시겠습니까? (0) | 2020.06.12 |