2012-10-27 80 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

感谢您的宝贵努力,以解决此问题.. –

相关问题