jQuery checkbox change and click event
1 2 3 4 5 6 7 8 9 10 11 12 13 14  | $(document).ready(function() { //set initial state. $('#textbox1').val($(this).is(':checked')); $('#checkbox1').change(function() { $('#textbox1').val($(this).is(':checked')); }); $('#checkbox1').click(function() { if (!$(this).is(':checked')) { return confirm("Are you sure?"); } }); });  | 
1 2 3  | <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"> <input type="checkbox" id="checkbox1"/><br /> <input type="text" id="textbox1"/>  | 
这里,
这会使事情处于不一致的状态,选中复选框时,文本框会显示"假"。
如何处理取消并保持文本框值与复选状态一致?
好吧,我看不到与我的答案匹配的答案,所以我会发布这个。在jsFiddle中测试并执行您所要求的操作。当单击与复选框关联的标签时,这种方法还有一个额外的好处,那就是触发。
原始答案:
1 2 3 4 5 6 7 8 9 10 11 12  | $(document).ready(function() { //set initial state. $('#textbox1').val($(this).is(':checked')); $('#checkbox1').change(function() { if($(this).is(":checked")) { var returnVal = confirm("Are you sure?"); $(this).attr("checked", returnVal); } $('#textbox1').val($(this).is(':checked')); }); });  | 
更新答案:
1 2 3 4 5 6 7 8 9 10 11 12  | $(document).ready(function() { //set initial state. $('#textbox1').val(this.checked); $('#checkbox1').change(function() { if(this.checked) { var returnVal = confirm("Are you sure?"); $(this).prop("checked", returnVal); } $('#textbox1').val(this.checked); }); });  | 
演示
使用
1 2 3 4 5 6  | $('#checkbox1').mousedown(function() { if (!$(this).is(':checked')) { this.checked = confirm("Are you sure?"); $(this).trigger("change"); } });  | 
如果你使用
1 2 3 4 5 6  |     <input type="checkbox" id="checkbox1" /> <label for="checkbox1">Checkbox label</label> <br /> <br /> The confirm result: <input type="text" id="textbox1" />  | 
在这种情况下,您可以使用:
1 2 3 4 5 6 7 8  | $('#OuterDivOrBody').delegate('#checkbox1', 'change', function () { // From the other examples if (!this.checked) { var sure = confirm("Are you sure?"); this.checked = !sure; $('#textbox1').val(sure.toString()); } });  | 
jquery 1.6.4的jfiddle示例
1 2 3 4 5 6 7 8  | $('#checkbox1').on('change', function() {  // From the other examples if (!this.checked) { var sure = confirm("Are you sure?"); this.checked = !sure; $('#textbox1').val(sure.toString()); } });  | 
最新jquery 2.x的jfiddle示例
- 添加了JSfiddle示例和带有可单击复选框标签的HTML
 
好。。为了避免头痛(这里已经过了午夜),我可以想出:
1 2 3 4 5 6  | $('#checkbox1').click(function() { if (!$(this).is(':checked')) { var ans = confirm("Are you sure?"); $('#textbox1').val(ans); } });  | 
希望它有帮助
对我来说,这很有效:
1 2 3 4 5 6 7  | $('#checkboxID').click(function () { if ($(this).attr('checked')) { alert('is checked'); } else { alert('is not checked'); } })  | 
给你
HTML
1  | <input id="ProductId_a183060c-1030-4037-ae57-0015be92da0e" type="checkbox" value="true">  | 
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15  |     $(document).ready(function () { $('input[id^="ProductId_"]').click(function () { if ($(this).prop('checked')) { // do what you need here alert("Checked"); } else { // do what you need here alert("Unchecked"); } }); });  | 
如果您使用的是icheck jquery,请使用以下代码
1 2 3  |  $("#CheckBoxId").on('ifChanged', function () { alert($(this).val()); });  | 
去掉更改事件,改为更改Click事件中文本框的值。不要返回确认的结果,而是在变量中捕获它。如果它为真,则更改该值。然后返回变量。
复选框单击并检查同一事件循环中的值是问题所在。
试试这个:
1 2 3 4 5 6 7 8 9 10 11  | $('#checkbox1').click(function() { var self = this; setTimeout(function() { if (!self.checked) { var ans = confirm("Are you sure?"); self.checked = ans; $('#textbox1').val(ans.toString()); } }, 0); });  | 
演示:http://jsfiddle.net/mrchief/jsuvv/6/
试试这个
1 2 3 4 5 6 7  | $('#checkbox1').click(function() { if (!this.checked) { var sure = confirm("Are you sure?"); this.checked = sure; $('#textbox1').val(sure.toString()); } });  | 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18  | $(document).ready(function() { //set initial state. $('#textbox1').val($(this).is(':checked')); $('#checkbox1').change(function() { $('#textbox1').val($(this).is(':checked')); }); $('#checkbox1').click(function() { if (!$(this).is(':checked')) { if(!confirm("Are you sure?")) { $("#checkbox1").prop("checked", true); $('#textbox1').val($(this).is(':checked')); } } }); });  | 
1 2 3 4 5 6 7 8 9 10 11 12  | // this works on all browsers. $(document).ready(function() { //set initial state. $('#textbox1').val($(this).is(':checked')); $('#checkbox1').change(function(e) { this.checked = $(this).is(":checked") && !!confirm("Are you sure?"); $('#textbox1').val(this.checked); return true; }); });  | 
1 2 3 4 5 6 7 8 9 10 11 12  | $('#checkbox1').click(function() { if($(this).is(":checked")) { var returnVal = confirm("Are you sure?"); $(this).attr("checked", returnVal); } $('#textbox1').val($(this).is(':checked')); }); <input type="checkbox" id="checkbox1" /> <input type="text" id="textbox1" />  | 
按名称获取无线值
1 2 3 4 5 6 7  |  $('input').on('className', function(event){ console.log($(this).attr('name')); if($(this).attr('name') =="worker") { resetAll(); } });  | 
我不知道为什么每个人都把这件事搞得这么复杂。这就是我所做的。
1  | if(!$(this).is(":checked")){ console.log("on"); }  | 
只需使用点击事件我的复选框ID是checkall
1 2 3 4 5 6 7  |      $('#CheckAll').click(function () { if ($('#CheckAll').is(':checked') == true) { alert(";)"); } }  | 
回答迟了,但你也可以使用
1 2 3 4  | $('#check').on('change', function() { var checked = this.checked $('span').html(checked.toString()) });  | 
1 2  | <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"> <input type="checkbox" id="check"> <span>Check me!</span>  |