开发者

Addclass to parent based on input condition

开发者 https://www.devze.com 2023-03-08 11:15 出处:网络
<label><input type=\"checkbox\" name=\"\" /> Normal</label> <label><input type=\"checkbox\" name=\"\" checked=\"checked\" /> Checked</label>
<label><input type="checkbox" name="" /> Normal</label>
<label><input type="checkbox" name="" checked="checked" /> Checked</label>
<label><input type="checkbox" name="" disabled="disabled" /> Disabled</label>

If the checkbox is 'checked', its label should have class 'checked', same for 'disabled'.

I tried this, but doesn't seem to work:

$("input[type=checkbox][checked]").each( 
    function(开发者_JAVA技巧) { 
       $(this).parent().addClass('checked');
    } 
);

http://jsfiddle.net/eJfT6/

Thanks for your help!


You can test this:

$('input:checkbox').each(function(){

    if($(this).is(':checked')){

        $(this).parent().addClass('checked');
    }

    if($(this).is(':disabled')){

        $(this).parent().addClass('disabled');
    }
});


try this one....

$("input[type=checkbox][checked]").each(
    function() {

        $(this).parent().addClass('checked');

    }
);
// for disabled 
$("input[type=checkbox]").each(
    function() {
        if( $(this).attr('disabled')){
           alert("in side");
            $(this).parent().addClass('checked');
        }

    }
);

you can merge both function i have seprated it just for your understanding.
http://jsfiddle.net/Htcjb/

0

精彩评论

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