jQuery检查是否已选中css custom复选框

时间:2013-05-09 11:04:28

标签: jquery css

我必须使用以下css构建自定义复选框:

  input[type="checkbox"] {
      display:none;
  }
  input[type="checkbox"] + label span {
      display:inline-block;
      width:19px;
      height:19px;
      margin:-1px 4px 0 0;
      vertical-align:middle;
      background:url("../image/checkbox.png") 0 -20px no-repeat;
      cursor:pointer;
  }
  input[type="checkbox"]:checked + label span {
      background:url("../image/checkbox.png") 0 -1px no-repeat;
  }

但是现在我在使用以下功能检查复选框是否已选中时遇到问题:

var radio_check = $('#one input[type="checkbox"]');

    function add_color(element, color){
        element.css('background-color', color);
    }

    function checkEmailFormat(){
        if(emailReg.test(emailaddressVal)) {
            add_color(email, red);
        }
        else {
            return true;
        }
    }

    function check_Radio_Checkb(){
        if(!radio_check.is(':checked')){
            alert('no');
        }
        else{
            alert('yes');
            // return true;
        }
    }

    function validate_form(){

        $('form input:not(.email input), textarea').each(function(){
            if ($(this).val() == '') {
                add_color($(this), red);
            }
            else {
                add_color($(this), white);
            }

        });

        return (checkEmailFormat() && true);

    }


                            <div class="checkbox_wrapper">

                                <input type="checkbox" id="one" />
                                <label for="one">
                                    <span></span>
                                    I agree with the terms and conditions and privacy policy
                                </label>
                                <input type="checkbox" id="two" />
                                <label for="two">
                                    <span></span>
                                    Join the Krušovice VIPs for the latest news, competitions and promotions. 
                                </label>

                            </div>

2 个答案:

答案 0 :(得分:1)

您的选择器错误,应该是:

var radio_check = $('#one');

目前,您正尝试使用#one

选择input[type="checkbox"]的后代

答案 1 :(得分:1)

嘿试试这个JS代码而不是你的。它会按预期给你结果。

$('input[type="checkbox"]').change(function(){
check_Radio_Checkb($(this));

});
function add_color(element, color){
    element.css('background-color', color);
}

function checkEmailFormat(){
    if(emailReg.test(emailaddressVal)) {
        add_color(email, red);
    }
    else {
        return true;
    }
}

function check_Radio_Checkb(rdb_new){
    if(!rdb_new.is(':checked')){
        alert('no');
    }
    else{
        alert('yes');
        // return true;
    }
}

function validate_form(){

    $('form input:not(.email input), textarea').each(function(){
        if ($(this).val() == '') {
            add_color($(this), red);
        }
        else {
            add_color($(this), white);
        }

    });

    return (checkEmailFormat() && true);

}

希望它能奏效。