【问题标题】:jquery CheckBox required需要 jquery 复选框
【发布时间】:2009-11-10 14:54:18
【问题描述】:

我怎样才能使一个复选框成为必需的?

我的代码

aspx:

<asp:CheckBox ID="cbIsAgree" CssClass="{CheckedBox:true, messages:{CheckedBox:'check me!.'}}" runat="server" Text="check me!" />

javascript:

$.validator.addMethod("CheckedBox", function CheckedBox(value, element) {
        return $(element).is(':checked');
    }
    , 'check me!');

该代码不起作用...为什么?

【问题讨论】:

    标签: jquery validation checkbox


    【解决方案1】:

    required:true 添加到cssClass 中

    例如在普通的html中

    <input name="user" title="enter" class="{required:true,minlength:3}" />
    

    【讨论】:

    • 我正在寻找在 Asp.net 上需要复选框的示例 .... 这确实有效 ...
    【解决方案2】:

    我认为您对验证器的使用是错误的。 addMethod 函数所做的只是添加一个验证方法。它不会将该验证绑定到实际输入以进行验证。

    这是一个两步过程:

    • 定义一个说明如何验证元素的方法
    • 创建将输入链接到相关验证方法的规则列表

    查看此页面上的示例:

    http://docs.jquery.com/Plugins/Validation/Methods/required

    【讨论】:

    • 我正在寻找在 Asp.net 上需要复选框的示例 .... 这确实有效 ...
    【解决方案3】:
     $(document).ready(function() {
            $("#<%=cbIsAgree.ClientID %>").addClass("{required:true, messages:{required:'check me !.'}}");
    
        });
    

    这会起作用,因为如果你写这样的东西,.net 会将类放在跨度中

    <asp:chaeckbox runat="server" class=".....
    

    【讨论】:

      猜你喜欢
      • 2016-04-12
      • 2019-10-17
      • 2011-11-14
      • 1970-01-01
      • 1970-01-01
      • 2011-09-28
      • 1970-01-01
      • 2011-12-15
      • 2023-04-10
      相关资源
      最近更新 更多