2013-05-09 68 views
1

我不得不使用下面的CSS来建立一个自定义复选框:jQuery来检查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> 
+0

'var radio_check = radio_check'? – undefined 2013-05-09 11:06:08

+0

抱歉拼写错误var radio_check = $('#one input [type =“checkbox”]'); – Alex 2013-05-09 11:06:40

+0

你可以发布你的标记吗? – billyonecan 2013-05-09 11:07:43

回答

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); 

} 

希望它能起作用。

+0

这一个工作,我会修改更改功能,因为我从来没有用过它,谢谢 – Alex 2013-05-09 12:34:47

0

你的选择是错误的,它应该是:

var radio_check = $('#one'); 

目前,你TR ying选择一个#one的后代input[type="checkbox"]