【问题标题】:What can be used in place of dynamic id for form to use in jQuery validation?在 jQuery 验证中可以使用什么来代替动态 id 表单?
【发布时间】:2012-04-24 06:51:33
【问题描述】:

我有一个 aspx 内容页面,它由母版页继承。

在母版页中,我的主要内容占位符位于 <form> 下,

<form runat="server">
<div class="page">

    <div class="main">
        <asp:ContentPlaceHolder ID="MainContent" runat="server"/>
    </div>
    <div class="clear">
    </div>
</div>
<div class="footer">

</div>
</form>

以下是我的内容页面的代码:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<table border="0" cellpadding="3" cellspacing="3">
    <tr>
        <td colspan="2" class="header" style="color: Black;">
            LOGIN USER
        </td>
    </tr>
    <tr>
        <td align="right">
            <asp:Label ID="lblUserName" runat="server" Text="UserName: "></asp:Label>
        </td>
        <td align="left">
            <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td align="right">
            <asp:Label ID="lblPassword" runat="server" Text="Password: "></asp:Label>
        </td>
        <td align="left">
            <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td align="center" colspan="2">
            <asp:Button ID="btnSubmit" runat="server" Text="Submit" />&nbsp;
            <asp:Button ID="btnReset" runat="server" Text="Reset" />
        </td>
    </tr>
</table>
<div align="center" class="alertmsg">
</div>

虽然我将使用 jQuery 验证插件:

<script type="text/javascript" language="javascript">
    $(document).ready(function () {
            $('#ctl01').validate({
            rules: {
                    <%=txtUserName.UniqueID %>: {
                        minlength: 2,
                        required: true
                    },
                     <%=txtPassword.UniqueID %>: {                        
                        required: true,
                        email:true
                    }
                },
            messages: {
                    <%=txtUserName.UniqueID %>:{  
                        required: "* Required Field *",  
                        minlength: "* Please enter atleast 2 characters *"  
                    },
                    <%=txtPassword.UniqueID %>:{  
                        required: "* Required Field *",  
                        email: "* Please enter valid email address *"  
                    }       
                }

        });
});

</script>

txtUserName的动态id可以被&lt;%=txtUserName.UniqueID %&gt;暴露出来,但是为什么我要把form的id给$('#ctl01').validate呢? 我可以使用任何其他 id 代替#ctl01吗?

【问题讨论】:

    标签: jquery asp.net jquery-plugins


    【解决方案1】:

    .validate() 要求它来自,因为您在将它们发送到服务器之前进行验证以检查正确的值,即当用户提交表单并且只能提交表单并且只有通过它们才能防止从一开始就提交了错误的值..

    为了更清楚:

    您可以参考http://docs.jquery.com/Plugins/Validation/validate以获得清晰的描述。

    如果Control.UniqueID 不起作用,您可以使用Control.ClientID...

    【讨论】:

    • 在设计时使用表单的ID。如果是from1,那么写form1.ClientId。如果您的表单标签在母版页中,而 Jquery 在子页中,则只需使用“$('#aspnetForm').validate({...”。
    【解决方案2】:

    你可以使用:注意$('&lt;%=Form.UniqueID %&gt;').validate({$('&lt;%=Form.UniqueID %&gt;').validate({之间的区别

    <script type="text/javascript" language="javascript">
        $(document).ready(function () {
                $('#<%=Form.UniqueID %>').validate({
                rules: {
                        <%=txtUserName.UniqueID %>: {
                            minlength: 2,
                            required: true
                        },
                         <%=txtPassword.UniqueID %>: {                        
                            required: true,
                            email:true
                        }
                    },
                messages: {
                        <%=txtUserName.UniqueID %>:{  
                            required: "* Required Field *",  
                            minlength: "* Please enter atleast 2 characters *"  
                        },
                        <%=txtPassword.UniqueID %>:{  
                            required: "* Required Field *",  
                            email: "* Please enter valid email address *"  
                        }       
                    }
    
            });
    });
    
    </script>
    

    【讨论】:

    • $('') 不起作用,你要求什么?
    • 对不起,我错过了&lt;%=Form.UniqueID %&gt; 之前的#。更新了答案,请重新检查。
    猜你喜欢
    • 1970-01-01
    • 2011-12-14
    • 1970-01-01
    • 2015-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多