如何使用 jQuery 处理复选框的更改? [英] How to handle change of checkbox using jQuery?
本文介绍了如何使用 jQuery 处理复选框的更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些代码
<input type="checkbox" id="chk" value="value" />
<label for="chk">Value </label>
<br/>
<input type="button" id="But1" value="set value" />
<br />
<input type="button" id="But2" value="read checked" />
javascript:
javascript:
$(document).ready(function () {
console.log("Ready ...");
registerHandlers();
function registerHandlers() {
$('#But1').click(function () {
$('#chk').prop('checked', !$('#chk').is(':checked'));
});
$('#But2').click(function () {
var chk1 = $('#chk').is(':checked');
console.log("Value : " + chk1);
});
$('input[type="checkbox"]').change(function () {
var name = $(this).val();
var check = $(this).prop('checked');
console.log("Change: " + name + " to " + check);
});
}
});
如何使用 jQuery 处理复选框的更改?我需要让处理程序更改选中的所有复选框.
How to handle change of checkbox using jQuery ? I need to put the handler to change any checkboxes checked.
[更新]
有一个复选框和几个按钮.每个按钮都可以更改复选框.如何捕捉更改复选框的事件?
There is a checkbox and a few buttons. Each button can change check box. How to catch an event changing the checkbox?
[更新]
在此示例中,我需要处理更改复选框 jsfiddle.当我单击该框时,未显示消息确定"按钮.
I need handle change checkbox in this example jsfiddle. When I click on the box the message "OK" button is not shown.
推荐答案
使用 :checkbox
选择器:
Use :checkbox
selector:
$(':checkbox').change(function() {
// do stuff here. It will fire on any checkbox change
});
这篇关于如何使用 jQuery 处理复选框的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文