开发者

meaning of $(this) in an if(){}

开发者 https://www.devze.com 2023-03-08 21:56 出处:网络
in this rule: if($(\'fieldset[data-input-type=checkbox]\').find(\'inpu开发者_如何学JAVAt\').is(\':checked\'))

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').add‌​‌​Class('on');

and

$('fieldset:not(:has(:checkbox:checked)) .switch').add‌​‌​Class('off');


try jQuery v1.6 prop() method

if($('input:checkbox').prop('checked'))
{
    alert('This is checked');
}
0

精彩评论

暂无评论...
验证码 换一张
取 消