【问题标题】:jQuery Validation with Web Forms使用 Web 表单进行 jQuery 验证
【发布时间】:2026-01-08 10:45:02
【问题描述】:

我不知道怎么了,但是当我点击文本框时,我总是得到

未捕获的类型错误:无法读取未定义的属性“设置”

这是我的代码(为清楚起见进行了简化):

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

<div id="MessageForm">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

    <asp:Button ID="Button1" runat="server" Text="doPostBack" />
</div>

<script type="text/javascript">
    $(document).ready(function () {
        $('#MessageForm').validate({
            rules: {
                <%= TextBox1.ClientID %>: {
                        required: true,
                        minlength: 6,
                        maxlength: 25
                    }
                },
                messages:{
                    <%= TextBox1.ClientID %>: {
                        required: "Please enter the address.",
                        minlength: $.validator.format("The address name must be at least {0} characters."),
                        maxlength: $.validator.format("The address name must not exceed {0} characters.")
                    }
                },
                errorClass: "error-label",
                wrapper: "li",
                errorLabelContainer: "#ErrorSection"
            });
        });
</script>

我已经试过了

  • &lt;div id="MessageForm"&gt; 更改为&lt;div id="MessageForm" runat="server"&gt;
  • $('#MessageForm').validate 更改为$('#MainContent_MessageForm').validate
  • &lt;%= TextBox1.ClientID %&gt; 更改为生成的客户端 ID (MainContent_TextBox1) 或生成的名称 (MainContent$Contenteplaceholder$ 等)

只有在使用母版页

时才会出现该问题

这可能是因为我引用的元素是&lt;div&gt; 吗? 我正在使用 Web 表单,所以我不能使用另一个 &lt;form&gt; 标签

【问题讨论】:

  • herehere 所述,您必须为jQuery Validate 插件使用form。我还没有测试,但你试过validator.element()
  • @SyedAliTaqi 谢谢,我可以轻松地将元素更改为&lt;fieldset&gt;,但这还不够。我发现现在人们无法轻松地将 jquery 验证集成到 web 表单上,这令人难以置信。我只是要使用标准验证器,谢谢。请发表你的答案

标签: jquery asp.net


【解决方案1】:

herehere 所述,您必须为jQuery Validate 插件使用form。我无法确定满足您要求的特定插件,但您可以查看this

【讨论】:

    最近更新 更多