需要一种更好的方式来验证ASP.NET复选框使用jQuery验证插件? [英] Need a better way to validate ASP.NET checkboxes using a jquery validation plugin?

查看:86
本文介绍了需要一种更好的方式来验证ASP.NET复选框使用jQuery验证插件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

我想使用jQuery验证插件(在发现来验证一些与ASP.NET复选框:的 http://bassistance.de/jquery-plugins/jquery-plugin-validation/

I want to validate some ASP.NET-checkboxes using the jquery validation plugin (found at: http://bassistance.de/jquery-plugins/jquery-plugin-validation/)

什么它是所有关于:

这是不可能设置ASP.NET复选框的name属性(是吗?)。它会自动进行设置,当控制被渲染,可以使用检索

It's not possible to set the name attribute of ASP.NET checkboxes (or is it?). It'll automatically be set, when the control is rendered and can be retrieved using

<%= emailCheckBox.UniqueID %>

于是两个复选框如下:

So two checkboxes as the following:

<asp:CheckBox runat="server" ID="emailAcceptCheckBox" />Email<br />
<asp:CheckBox runat="server" ID="phoneAcceptCheckBox" />Phone<br />

将呈现为:

<INPUT id="ctl00_MainContentPlaceHolder_emailAcceptCheckBox" type="checkbox" name="ctl00$MainContentPlaceHolder$emailAcceptCheckBox">
<INPUT id="ctl00_MainContentPlaceHolder_phoneAcceptCheckBox" type="checkbox" name="ctl00$MainContentPlaceHolder$phoneAcceptCheckBox">

也许这是一个烂摊子ASP.NET与jQuery验证插件组合,但我preFER jQuery的验证插件,它正常工作与投入等多个领域。

Maybe it's a mess to mix ASP.NET with the jquery validation plugin, but I prefer the jquery validation plugin and it works fine with inputs and other fields.

问题是,jQuery的验证插件使用复选框的名称属性要组的复选框。此名称的属性应等于所有复选框组。

Problem is, that the jquery validation plugin wants to group the checkboxes using the name-attribute of checkboxes. This name attributes should be equal to all checkboxes and a group.

我所做的:

我使用jQuery添加一条规则,所有的复选框:

I added a rule to ALL checkboxes using jquery:

$("#[id*='AcceptCheckBox']").each(function() { $(this).rules("add", { minchecked: 1 }); });

并加入我自己的规则复选框检查:

And added my own rule checking for checkboxes:

jQuery.validator.addMethod("minchecked",
 function(value, element, param) {
     var noOfChecked = $("#[id*='AcceptCheckBox']:checked").length;
          return noOfChecked >= param;
     },
 "Error");

(此规则将适用于复选框其ID包括AcceptCheckBox,但在这个例子没关系)。

(This rule will apply for checkboxes whose IDs include AcceptCheckBox, but it's okay in this example).

当我打电话:

var result = $("#aspnetForm").validate().form();

它返回完美,如果我的验证是成功还是失败!

It returns perfectly if my validation succeeded or failed!

但是:

1)我不觉得这是一个非常干净,漂亮的解决方案。任何更好的建议?

1) I don't feel that this is a very clean and nice solution. Any better advice?

2)当我运行的validate()形式(),它会通过我的所有的复选框,并为每个检查所有其他复选框。这真的没有必要。我怎样才能避免这种情况?

2) When I run validate().form() it'll go through ALL my checkboxes and for each of them check all the other checkboxes. That's really not necessary. How can I avoid this?

推荐答案

总体而言,你的code看起来没什么问题。我想你已经处理干净的ASP:复选框限制(不能指定的aspname属性:复选框)。

Overall, your code does look fine to me. I think you have cleanly handled the asp:checkbox limitation (you can't specify 'name' attribute for asp:checkbox).

就个人而言,我会使用常规的HTML复选框。

Personally, I would use regular HTML checkbox.

<input type="checkbox" runat="server" ID="emailAcceptCheckBox" name="acceptCheckBox" value="emailAccepted" /><label for="emailAcceptCheckBox">Email</label><br />
<input type="checkbox" runat="server" ID="phoneAcceptCheckBox" name="acceptCheckBox" value="phoneAccepted" /><label for="phoneAcceptCheckBox">Email</label><br />

(我加了标签,标签中,使用户可以点击该标签(更大的目标)来检查/取消选中该复选框上。)

(I have added a label tag so that user can click on the label (bigger target) to check/uncheck the checkbox.)

以上将简化您的jQuery code。

Above will simplify your jQuery code.

在您的服务器端code,检查的Request.Form [acceptCheckBox],看看有什么检查,什么不是。

On your server side code, check Request.Form["acceptCheckBox"] to see what is checked and what is not.

这篇关于需要一种更好的方式来验证ASP.NET复选框使用jQuery验证插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆