ASP.Net CheckBoxList 的 jQuery 验证
在开始之前,我想声明一下,ASP.NET 为 CheckBoxLists 创建的代码可能是我见过的最糟糕的事情。
不管怎样,
我正在使用 jQuery 验证插件来验证我的 ASP.net 表单。需要验证一些复选框。它们由 CheckBoxList 控件生成。
<asp:CheckBoxList ID="CBContext" runat="server" RepeatColumns="2"
DataSourceID="sqlLibraryEnquiries" DataTextField="value" DataValueField="value" name="topic">
</asp:CheckBoxList>
这个控件会产生以下令人厌恶的 xHTML
<table id="MainContent_CBContext" name="topic">
<tr>
<td>
<input id="MainContent_CBContext_0" type="checkbox" name="ctl00$MainContent$CBContext$0" value="Business" /><label for="MainContent_CBContext_0">Business</label>
</td>
<td>
<input id="MainContent_CBContext_2" type="checkbox" name="ctl00$MainContent$CBContext$2" value="Legal" /><label for="MainContent_CBContext_2">Legal</label>
</td>
</tr>
<tr>
<td>
<input id="MainContent_CBContext_1" type="checkbox" name="ctl00$MainContent$CBContext$1" value="Business Development" /><label for="MainContent_CBContext_1">Business Development</label>
</td>
<td>
<input id="MainContent_CBContext_3" type="checkbox" name="ctl00$MainContent$CBContext$3" value="Library" /><label for="MainContent_CBContext_3">Library</label>
</td>
</tr>
</table>
我遇到的问题实际上是让 jQuery Validator 插件挂接到复选框列表中。在所有其他字段的规则部分中,我可以通过它们的名称找到它们 例如 ctl00$MainContent$tbActions: 但复选框都有不同的名称。
cb_selectone 规则未触发,因为我尝试验证的对象从未找到。 我尝试过以下标识符。 CBContext、ctl00$MainContent$CBContext、MainContent_CBContext 和复选框。
$("#Form1").validate({
rules: {
//WHAT GOES HERE???? --------->> CBContext or ctl00$MainContent$CBContext or MainContent_CBContext or checkboxes all don't work: {
cb_selectone: true
}
}
});
感谢您的帮助。
SM
Before I start I'd just like to state that the code created by ASP.NET for CheckBoxLists is probably the worst thing I've ever seen.
Anyway,
I am using the jQuery validation plugin to validate my ASP.net form. There is a requirement to validate some checkboxes. These are generated by a CheckBoxList control.
<asp:CheckBoxList ID="CBContext" runat="server" RepeatColumns="2"
DataSourceID="sqlLibraryEnquiries" DataTextField="value" DataValueField="value" name="topic">
</asp:CheckBoxList>
This control produces the following abomination of xHTML
<table id="MainContent_CBContext" name="topic">
<tr>
<td>
<input id="MainContent_CBContext_0" type="checkbox" name="ctl00$MainContent$CBContext$0" value="Business" /><label for="MainContent_CBContext_0">Business</label>
</td>
<td>
<input id="MainContent_CBContext_2" type="checkbox" name="ctl00$MainContent$CBContext$2" value="Legal" /><label for="MainContent_CBContext_2">Legal</label>
</td>
</tr>
<tr>
<td>
<input id="MainContent_CBContext_1" type="checkbox" name="ctl00$MainContent$CBContext$1" value="Business Development" /><label for="MainContent_CBContext_1">Business Development</label>
</td>
<td>
<input id="MainContent_CBContext_3" type="checkbox" name="ctl00$MainContent$CBContext$3" value="Library" /><label for="MainContent_CBContext_3">Library</label>
</td>
</tr>
</table>
The issue I am having is actually getting the jQuery Validator plugin to hook into the checkbox list. In my rules section for all the other fields I can get to them with their names
for example ctl00$MainContent$tbActions: but the checkboxes all have different names.
The cb_selectone rule isn't firing because the object I am trying to validate is never found.
I have tried the following identifiers. CBContext, ctl00$MainContent$CBContext, MainContent_CBContext and checkboxes.
$("#Form1").validate({
rules: {
//WHAT GOES HERE???? --------->> CBContext or ctl00$MainContent$CBContext or MainContent_CBContext or checkboxes all don't work: {
cb_selectone: true
}
}
});
Thanks for your help.
SM
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
列表的呈现方式使其变得复杂。我会考虑创建您自己的验证方法,并使用根控件的 ID,并让验证方法解析内部子项:
How the list renders makes it complex. I would consider creating your own validation method, and use the root control's ID, and have the validation method parse the inner children:
你有没有尝试过这样的事情:
Did you try something like this:
是选择器,它将抓取所有选中的复选框,
我用它来进行简单的验证,例如:
我想你可以这样做
is the selector that'll grab all checked checkboxes
I've used this for simple validation like:
I imagine you could just do
我在JAVASCRIPT方法上做了一个小小的调整:
I made a small adjustment in the JAVASCRIPT method:
好吧,我解决了......
我所做的是创建一个新的验证器方法,该方法获取与 MainContent_CBContext 的正则表达式匹配的输入类型的所有对象。这将返回所有复选框的数组。
然后循环数组并检查 attr 是否已检查。如果其中任何一个被设置,则返回为 true。
我陷入困境的部分是找到一个对象来触发 addMethod 代码。
最后我只是使用...
这意味着标签放置在该字段旁边,它纯粹是装饰性的。重要的是验证器代码最终绑定到真实对象并正确触发。
SM
Ok I Solved it......
What I did was create a new validator method that gets all the objects of type input that match a regex of MainContent_CBContext. This returns an array of all the checkboxes.
Then loop round the array and check if the attr is checked. If any of them are then set the return as true.
The part I was stuck on was finding an object to fire off the addMethod code.
In the end I just used...
This meant that the label is placed next to this field, it's purely cosmetic. The important thing is the validator code was finally bound to a real object and fired correctly.
SM