2014-04-07 84 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