如何<input type="checkbox" />
使用 jQuery 捕获选中/取消选中事件?
问问题
252862 次
9 回答
179
<input type="checkbox" id="something" />
$("#something").click( function(){
if( $(this).is(':checked') ) alert("checked");
});
编辑:当复选框因单击以外的其他原因(例如使用键盘)发生更改时,这样做不会被捕获。为避免此问题,请收听change
而不是click
.
要以编程方式检查/取消选中,请查看为什么我的复选框更改事件没有被触发?
于 2009-09-21T15:50:11.320 回答
46
如果我们在输入复选框上附加了一个标签,点击会影响一个标签吗?
我认为最好使用 .change() 函数
<input type="checkbox" id="something" />
$("#something").change( function(){
alert("state changed");
});
于 2012-01-04T08:31:00.707 回答
29
使用:checked选择器来确定复选框的状态:
$('input[type=checkbox]').click(function() {
if($(this).is(':checked')) {
...
} else {
...
}
});
于 2009-09-21T15:51:10.450 回答
14
对于 JQuery 1.7+ 使用:
$('input[type=checkbox]').on('change', function() {
...
});
于 2014-06-18T20:56:10.993 回答
7
此代码可以满足您的需要:
<input type="checkbox" id="check" >check it</input>
$("#check").change( function(){
if( $(this).is(':checked') ) {
alert("checked");
}else{
alert("unchecked");
}
});
另外,您可以在jsfiddle上查看
于 2016-09-07T13:35:33.527 回答
4
使用下面的代码片段来实现这一点:
$('#checkAll').click(function(){
$("#checkboxes input").attr('checked','checked');
});
$('#UncheckAll').click(function(){
$("#checkboxes input").attr('checked',false);
});
或者您可以对单个复选框执行相同操作:
$('#checkAll').click(function(e) {
if($('#checkAll').attr('checked') == 'checked') {
$("#checkboxes input").attr('checked','checked');
$('#checkAll').val('off');
} else {
$("#checkboxes input").attr('checked', false);
$('#checkAll').val('on');
}
});
演示:http: //jsfiddle.net/creativegala/hTtxe/
于 2011-11-16T09:24:23.950 回答
3
根据我的经验,我不得不利用事件的 currentTarget:
$("#dingus").click( function (event) {
if ($(event.currentTarget).is(':checked')) {
//checkbox is checked
}
});
于 2015-04-27T05:30:03.557 回答
2
使用 click 事件以获得与 MSIE 的最佳兼容性
$(document).ready(function() {
$("input[type=checkbox]").click(function() {
alert("state changed");
});
});
于 2009-09-21T15:48:23.540 回答
-1
$(document).ready(function(){
checkUncheckAll("#select_all","[name='check_boxes[]']");
});
var NUM_BOXES = 10;
// last checkbox the user clicked
var last = -1;
function check(event) {
// in IE, the event object is a property of the window object
// in Mozilla, event object is passed to event handlers as a parameter
event = event || window.event;
var num = parseInt(/box\[(\d+)\]/.exec(this.name)[1]);
if (event.shiftKey && last != -1) {
var di = num > last ? 1 : -1;
for (var i = last; i != num; i += di)
document.forms.boxes['box[' + i + ']'].checked = true;
}
last = num;
}
function init() {
for (var i = 0; i < NUM_BOXES; i++)
document.forms.boxes['box[' + i + ']'].onclick = check;
}
HTML:
<body onload="init()">
<form name="boxes">
<input name="box[0]" type="checkbox">
<input name="box[1]" type="checkbox">
<input name="box[2]" type="checkbox">
<input name="box[3]" type="checkbox">
<input name="box[4]" type="checkbox">
<input name="box[5]" type="checkbox">
<input name="box[6]" type="checkbox">
<input name="box[7]" type="checkbox">
<input name="box[8]" type="checkbox">
<input name="box[9]" type="checkbox">
</form>
</body>
于 2009-09-21T15:57:36.863 回答