[英]jquery checkbox checked toggle when click of a button
I want to toggle the checkbox on click of button.我想在单击按钮时切换复选框。
Here is my jQuery这是我的 jQuery
$("#choose_address2").on("click", function () {
console.log("click!!! address2");
var $checkbox = $(this).find(':checkbox');
var checkBoxes = $("input[name=checkbox]]");
checkBoxes.prop("checked", !checkBoxes.prop("checked"));
});
Here is my Input这是我的输入
<span id="choose_address2" class="btn btn-info">
<i class="fa fa-plus-circle"></i>เพิ่มที่อยู่
<input type="checkbox" name="choose_address" {{#if customer.addresses}}checked{{/if}} id="choose_address1" hidden>
</input></span>
By default the button is checked.默认情况下,该按钮处于选中状态。 however when i click the span with id
choose_address2
I want to change the state.但是,当我单击 ID 为
choose_address2
的跨度时,我想更改状态。 Any suggestions will be highly appreciated.任何建议将不胜感激。
Try This尝试这个
$("#choose_address2").on("click", function () {
console.log("click!!! address2");
var $checkbox = $(this).find(':checkbox');
var checkBoxes = $("input[type='checkbox']");
if(checkBoxes.prop("checked")==true)
checkBoxes.prop("checked", false);
else
checkBoxes.prop("checked", true)
});
Or This also work或者这也有效
$("#choose_address2").on("click", function () {
console.log("click!!! address2");
var $checkbox = $(this).find(':checkbox');
var checkBoxes = $("input[type='checkbox']");
(checkBoxes.prop("checked")==true)?checkBoxes.prop("checked", false):checkBoxes.prop("checked", true);
});
Heres an example这是一个例子
HTML HTML
<input type="checkbox" class="toggle" checked>
<input type="checkbox" class="toggle">
<button id="choose_address2">
Toggle checkbox
</button>
JQ杰昆
$('#choose_address2').click(function(){
$('.toggle').each(function(){
$(this).prop('checked', !$(this)[0].checked);
})
})
Demo http://jsfiddle.net/mPstq/94/演示http://jsfiddle.net/mPstq/94/
Thank you all for you suggestion..谢谢大家的建议。。
This did the trick for me :这对我有用:
$("#choose_address2").click(function(){
var checkBox = $("#choose_address1");
console.log("click!!! address2");
checkBox.trigger('click');
});
$("#choose_address2").on("click", function() {
var $checkbox = $(this).find(':checkbox');
$checkbox.prop("checked", !$checkbox.prop("checked"));
});
解决这个问题的最简单方法:
$checkbox.prop( 'checked', ! $checkbox.is( ':checked' ) );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.