2014-04-07 89 views
1

如何在验证之前控制两个复选框?当我尝试它只有1,但与2我块。jquery中的两个验证复选框

HTML代码:

<div id="form-validation"> 
    <input class="ident_pro_cgv" id="ident_pro_cgv" name="ident_pro_cgv" value="1" type="checkbox" style="width:20px;"> 
    <input class="ident_pro_habil" id="ident_pro_habil" name="ident_pro_habil" value="1" type="checkbox" style="width:20px;"> 
    <div class='ident_pro_habilME' style="color:#fd0082;"></div> 

JQUERY

$(document).ready(function() { 
    $('#registerButton').click(function(){ 

     var ident_pro_cgv=$('#ident_pro_cgv').val(); 
     var ident_pro_habil=$('#ident_pro_habil').val(); 

     if ($("#ident_pro_cgv:checked || #ident_pro_habil:checked").length == 0){ 
      $('.ident_pro_habilME').text("Veuillez accepter les conditions générales et confirmer votre habilitation."); 
      return false; 
     } 
    }); 
}); 
+0

非常感谢SUPER – user3507141

回答

1

更改为:

if ($("#ident_pro_cgv:checked || #ident_pro_cgv:checked").length == 0){ 
     $('.ident_pro_habilME').text("Veuillez accepter les conditions générales et confirmer votre habilitation."); 
     return false; 
    } 

此:

if ($("#ident_pro_cgv:checked").length == 0 || $("#ident_pro_cgv:checked").length == 0){ 
     $('.ident_pro_habilME').text("Veuillez accepter les conditions générales et confirmer votre habilitation."); 
     return false; 
    } 

Working Demo

或该:

if ($("#ident_pro_cgv:checked, #ident_pro_cgv:checked").length == 0){ 
     $('.ident_pro_habilME').text("Veuillez accepter les conditions générales et confirmer votre habilitation."); 
     return false; 
    } 

Working Demo

+0

'$(“#ident_pro_cgv:checked,#ident_pro_cgv:checked”).length'会更好。 – MacMac

+0

@BurningtheCodeigniter:没有它没有。这不能确保他们是否被检查。 –

+0

比较两个版本:http://jsfiddle.net/zNsU4/,http://jsfiddle.net/zNsU4/1/ – MacMac