I want to toggle the checkbox on click of button.
Here is my 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. 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
<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);
})
})
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' ) );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.