meaning of $(this) in an if(){}
in this rule:
if($('fieldset[data-input-type=checkbox]').find('inpu开发者_如何学JAVAt').is(':checked'))
{
alert($(this));
}
why do I get "object Object" as an alert and not the fieldset? How would I do a specific action for each fieldset with a checked input in it and not all of them?
This should do it
$('fieldset:has(:checkbox:checked)').each(function(){
alert(this);
});
demo http://jsfiddle.net/gaby/hVTBF/
or if you indeed have to filter only to fieldset
that have the attribute data-input-type
then use
$('fieldset[data-input-type=checkbox]:has(:checkbox:checked)').each(function(){
alert(this);
});
Update after comment
Since you just want to run more jQuery methods on the objects you can directly do that in your initial selector and avoid the each
which runs a function for each item.
$('fieldset:has(:checkbox:checked) .switch').addClass('on');
and
$('fieldset:not(:has(:checkbox:checked)) .switch').addClass('off');
try jQuery v1.6 prop() method
if($('input:checkbox').prop('checked'))
{
alert('This is checked');
}
精彩评论