开发者

(jQuery) Different results based on checked attribute

开发者 https://www.devze.com 2023-03-19 04:18 出处:网络
I\'m wanting to change the background color on something depending on if it\'s child is checked or not, AND on whether or not it has the checked=\'checked\' attribute in the HTML.

I'm wanting to change the background color on something depending on if it's child is checked or not, AND on whether or not it has the checked='checked' attribute in the HTML.

Basically, this is what I want:

  • If it's checked by default (with checked='checked') and the user unticks it, red background
  • If it's checked by default, but no longer checked (user unticked it), but the user ticks it again, green background
  • If it's unchecked by default, and the user checks it, blue background
  • If it's unchecked by default, and the user checks it and then unchecks it, white background...

This is what I've got so far:

    $(document).ready(function(){
        $("#memberForm input").change(开发者_开发知识库function() {
            if($(this).is(":checked") == true ) {
                $(this).closest('dt').css({"background" : "green"});
            }
            else {
                $(this).closest('dt').css({"background" : "red"}); }
        });
    });

I don't know how to incorporate the default value in the results...


You could add a class to all checked inputs on initial load:

$("input:checked").addClass("waschecked");

Then just check if that input has the class to tell if it was checked on initial load:

$(this).hasClass("waschecked")
0

精彩评论

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