[英]jQuery toggling the checked property on a checkbox
I want to check or uncheck a buttonstyled checkbox with the following code:我想使用以下代码选中或取消选中按钮样式的复选框:
<ul id="ids_ul" class="nav nav-list">
<li>
<div class="btn-group-toggle" data-toggle="buttons">
<label class="btn btn-info group_toggle" id="id_label1" for="id_checkboxes1">
<input type="checkbox" value="1" id="id_checkboxes1" name="ids[]" autocomplete="off"/> One</label>
</div>
</li>
<li>
<div class="btn-group-toggle" data-toggle="buttons">
<label class="btn btn-success group_toggle" id="id_label2" for="id_checkboxes2">
<input type="checkbox" value="2" id="id_checkboxes2" name="ids[]" autocomplete="off"/> Two</label>
</div>
</li>
<li>
<div class="btn-group-toggle" data-toggle="buttons">
<label class="btn btn-info group_toggle" id="id_label3" for="id_checkboxes3">
<input type="checkbox" value="3" id="id_checkboxes3" name="ids[]" autocomplete="off"/> Three</label>
</div>
</li>
</ul>
let checkbox_state = "";
$(".group_toggle").click(function(){
$(this).toggleClass("btn-info btn-success");
checkbox_state = $(this).children("input:first");
console.log("Initial checkbox_state: " + checkbox_state.prop("checked"));
if($(checkbox_state).prop("checked") == true) {
$(checkbox_state).prop("checked", false);
console.log("Checked after click: " + checkbox_state.prop("checked"));
} else {
$(checkbox_state).prop("checked", true);
console.log("Checked after click: " + checkbox_state.prop("checked"));
}
});
After four clicks on Two
the log shows:四次单击“ Two
,日志显示:
Initial checkbox_state: true
Checked after click: false
Initial checkbox_state: true // why still true?
Checked after click: false
Initial checkbox_state: false
Checked after click: true
Initial checkbox_state: true
Checked after click: false
On the 3rd click the uncheck works, but the button-class is now the opposite.第三次单击取消选中有效,但按钮类现在相反。 If i click on One
or Three
to check the checkbox it works in the first try.如果我单击“ One
或“ Three
来检查复选框,它会在第一次尝试时起作用。
Does someone know what happens here?有人知道这里发生了什么吗?
Is there a more elegant way to verify the checkbox property checked
?有没有验证复选框属性更优雅的方式checked
?
You can use e.preventDefault()
to stop the default behavior of the clicked element, which seemed to cause the click event to fire twice when using twitter-bootstrap-3您可以使用e.preventDefault()
来停止被点击元素的默认行为,这似乎导致使用 twitter-bootstrap-3 时点击事件触发两次
Also, checkbox_state
seems like a confusing name for the element, being as it actually stores the checkbox itself and you use prop('checked')
to get it's state - I've updated this and change it to be inside the function, as you did not need this to be globally defined此外, checkbox_state
似乎是元素的一个令人困惑的名称,因为它实际上存储了复选框本身,并且您使用prop('checked')
来获取它的状态 - 我已经更新了它并将其更改为在函数内部,就像您一样不需要全局定义
$('.group_toggle').click(function(e) { e.preventDefault(); $(this).toggleClass('btn-info btn-success'); let $checkbox = $(this).children('input:first'); let checked = $checkbox.prop('checked'); console.log(`Initial checkbox_state: ${checked}`); // Invert checked state $checkbox.prop('checked', !checked); console.log(`Checked after click: ${$checkbox.prop('checked')}`); });
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.css" rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul id="ids_ul" class="nav nav-list"> <li> <div class="btn-group-toggle" data-toggle="buttons"> <label class="btn btn-info group_toggle" id="id_label1" for="id_checkboxes1"> <input type="checkbox" value="1" id="id_checkboxes1" name="ids[]" autocomplete="off"/>One </label> </div> </li> <li> <div class="btn-group-toggle" data-toggle="buttons"> <label class="btn btn-success group_toggle" id="id_label2" for="id_checkboxes2"> <input type="checkbox" value="2" id="id_checkboxes2" name="ids[]" autocomplete="off"/>Two </label> </div> </li> <li> <div class="btn-group-toggle" data-toggle="buttons"> <label class="btn btn-info group_toggle" id="id_label3" for="id_checkboxes3"> <input type="checkbox" value="3" id="id_checkboxes3" name="ids[]" autocomplete="off"/>Three </label> </div> </li> </ul>
To somewhat expand on @Light's answer稍微扩展@Light 的回答
The problem that I can see is the order of events happening and you trying to manually control the state of a checkbox.我能看到的问题是事件发生的顺序以及您试图手动控制复选框的状态。 When you wrap a checkbox with a label (or use the for="#id" on the label) it delegates a click to the checkbox whenever the label is clicked (which is why it gets triggered twice).当您用标签包装复选框(或在标签上使用 for="#id" )时,只要单击标签,它就会将单击委托给复选框(这就是它被触发两次的原因)。 The other thing is your initial state of the buttons was incorrect, button Two had the class btn-success but was not active and the related checkbox wasn't checked.另一件事是您的按钮的初始状态不正确,按钮二具有类 btn-success 但未处于活动状态且未选中相关复选框。 See below for complete resolution请参阅下面的完整解决方案
let checkbox_state = ""; $(".group_toggle").click(function(e){ e.preventDefault(); $(this).toggleClass("btn-info btn-success"); checkbox_state = $(this).children("input:first"); console.log("Initial checkbox_state: " + checkbox_state.prop("checked")); if($(checkbox_state).prop("checked") == true) { checkbox_state.prop('checked', false); console.log("Checked after click: " + checkbox_state.prop("checked")); } else { checkbox_state.prop('checked', true); console.log("Checked after click: " + checkbox_state.prop("checked")); } });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet"/> <ul id="ids_ul" class="nav"> <li> <div class="btn-group-toggle" data-toggle="buttons"> <label class="btn btn-info group_toggle" id="id_label1" for="id_checkboxes1"> <input type="checkbox" value="1" id="id_checkboxes1" name="ids[]" autocomplete="off"/> One</label> </div> </li> <li> <div class="btn-group-toggle" data-toggle="buttons"> <label class="btn btn-success group_toggle active" id="id_label2" for="id_checkboxes2"> <input type="checkbox" value="2" id="id_checkboxes2" name="ids[]" autocomplete="off" checked=""/> Two</label> </div> </li> <li> <div class="btn-group-toggle" data-toggle="buttons"> <label class="btn btn-info group_toggle" id="id_label3" for="id_checkboxes3"> <input type="checkbox" value="3" id="id_checkboxes3" name="ids[]" autocomplete="off"/> Three</label> </div> </li> </ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.