2012-10-27 83 views
1

我有一些複選框,這樣的事情:檢查至少一個複選框驗證jQuery中

<form name="submit-form" name="submit-form"> 
<label>property1</label> 
<input type="checkbox" name="property1"><br> 
<label>property2</label> 
<input type="checkbox" name="property2"><br> 
<label>property3</label> 
<input type="checkbox" name="property3"><br> 
<label>property4</label> 
<input type="checkbox" name="property4"><br> 
<label>property5</label> 
<input type="checkbox" name="property5"><br> 
<label>property6</label> 
<input type="checkbox" name="property6"><br> 
<input type="submit"> 
</form> 

我想驗證一次ATLEAST一個複選框。 PLZ幫助我。在此先感謝

+0

地方實際的HTML請 – rahul

回答

6
$("#YourbuttonId").click(function(){ 
    if($('#YourTableId').find('input[type=checkbox]:checked').length == 0) 
    { 
     alert('Please select atleast one checkbox'); 
    } 
}); 

更新:

我看到你的代碼,好像你正在使用jQuery驗證插件,在這種情況下,這篇文章將幫助你

,並嘗試讓你的複選框同名其中存在一組

http://forum.jquery.com/topic/check-that-at-least-1-checkbox-is-checked-using-jquery-validate-plugin

+0

但我有一些其他複選框以相同的形式... –

+0

它的工作原理,只有當我刷新頁面,而不是提交表單。 –

+0

@ Ravneet'Abid」我已經更新了我的代碼,看看現在 – rahul

1

我換他們我只爲更便於選擇一個div和Na容器,那麼你可以使用:

if($('#wrapperID input:checked').length > 0) { 
    'at least one is checked 
} 
+0

這些複選框已經在表... –

+0

確定。然後把ID放在桌子上。 –

0

試試這個

$('form').submit(function(e) { 
    if($("input:checked").length == 0){ 
    alert('please checked atleast one'); 
    } 
});​ 
0

看到這個演示:http://jsfiddle.net/RGUTv/您的具體情況的演示在這裏:http://jsfiddle.net/J98Ua/

我老在這裏迴應:validation for at least one checkbox

希望其他符合需求! :)

代碼

$(document).ready(function() { 
    $('#my-form').submit(function() { 
     amIChecked = false; 
     $('input[type="checkbox"]').each(function() { 
      if (this.checked) { 
       amIChecked = true; 
      } 
     }); 
     if (amIChecked) { 
      alert('atleast one box is checked'); 
     } 
     else { 
      alert('please check one checkbox!'); 
     } 
     return false; 
    }); 
});​ 
+0

它不起作用。我知道ü在此之前解決很多的複選框驗證的問題,但它不工作,我不希望它在警戒...... –

+0

@ Ravneet'Abid」酷酷的男人,笑可以使你輕拂的問題的演示請問JQ碼?萊米知道哪部分實際上不起作用。 –

+0

感謝您的寶貴努力,以解決此問題.. –

相關問題