复选框的验证 [英] validation for checkboxes
本文介绍了复选框的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<tr>
<td height="12" align="left" class="cont">
<asp:CheckBox ID="chkacsoutreach" runat="server" />Acs Outreach
</td>
</tr>
<tr>
<td height="12" align="left" class="cont">
<asp:CheckBox ID="chkaceportal" runat="server" /> Ace Portal
</td>
</tr>
我以以下方式选中了一些复选框.如果未选中至少1个复选框并且不应将值输入数据库,则应该收到一条消息.这是我需要的验证.我编写了如下的javascript代码:但是"controls []"的名称应该是一个包含所有复选框的组名.我不知道在此输入什么名称.任何人都可以帮帮我吗
I took some checkboxes in the following way.I should get a message if atleast 1 checkbox is not checked and the values should not be entered into the database.This is the validation i needed.I have written the javascript code as below:but instead of ''controls[]'' there should be a groupname which combines all the checkboxes.Iam not understanding what name to give here.Can any one please help me out
<script type="text/javascript" language="javascript">
function validate() {
var chks = document.getElementsByName('controls[]');
var hasChecked = false;
for (var i = 0; i < chks.length; i++) {
if (chks[i].checked) {
hasChecked = true;
break;
}
}
if (hasChecked == false) {
alert("Please select at least one.");
return false;
}
return true;
}
</script>
推荐答案
脚本稍有变化.
script change a little.
<script src="jquery-1.4.1.js" type="text/javascript" language="javascript"></script>
<script type="text/javascript" language="javascript">
function validate() {
var hasChecked = false;
var chkBox =
(':checkbox'); for(i = 0; i < chkBox.length ; i ++) { (chkBox [i] .checked) { 已检查 = 休息; } 如果 (!hasChecked) { 一个.); } re打开 true; } &l t;/script >
(':checkbox'); for (i = 0; i < chkBox.length; i++) { if (chkBox[i].checked) { hasChecked = true; break; } } if (!hasChecked) { alert("Please select at least one."); return false; } return true; } </script>
还要检查此链接
:选中的选择器 [
also check this link
:checked Selector[^]
[/Edit]
为什么不放入Validator控件并将所有复选框放入分配给该Validator的一组中?
现在,如果您想更改JS,请查看此线程 [ ^ ]和 ^ ]线程.
Why not put a Validator control and put all the checkboxes into one group assigned to that validator?
For now, if you want changes in JS, then have a look at this thread[^] and this [^]thread.
这篇关于复选框的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文