我想判断当用户点击复选框时,复选框是否被取消选中.这是因为我想在用户取消选中复选框时进行验证.因为至少需要选中一个复选框.因此,如果他取消选中最后一个,那么它会自动再次判断自己.
使用jQuery,我可以很容易地找到它是否已被判断:
$('#check1').click(function() {
if($(this).is(':checked'))
alert('checked');
else
alert('unchecked');
});
但我实际上只想有一个if语句来判断复选框是否被取消选中.
所以我想我可以用下面的代码做到这一点:
$('#check2').click(function() {
if($(this).not(':checked'))
alert('unchecked');
else
alert('checked');
});
But this will always show the 'unchecked' message. Not really what i was expecting...
demo: http://jsfiddle.net/tVM5H/
So eventually i need something like:
$('#check2').click(function() {
if($(this).not(':checked')) {
// Got unchecked, so something!!!
}
});
But obviously this doesn't work. I rather don't want to use the first example, because then i'd have an unnecessary 'else' statement when i only need one 'if' statement.
So first thing, is this a jQuery bug? Cause to me it's unexpected behaviour. And second, anyone any ides for a good alternative?