【问题标题】:How can I add a "Required" attribute to a dynamically-created (C#) element?如何向动态创建的 (C#) 元素添加“必需”属性?
【发布时间】:2015-10-20 22:16:50
【问题描述】:

我想将一些 TextBoxes/文本输入设为“必需”,以便我可以使用 Validate jQuery 验证插件。

我试过了:

boxPayeeName = new TextBox
{
    CssClass = "finaff-webform-field-input",
    ID = "payeeName",
    Required = true
};

...但是那里没有识别出“必需”;所以我尝试了这个:

boxPayeeName = new TextBox
{
    CssClass = "finaff-webform-field-input",
    ID = "payeeName"
};
boxPayeeName.Attributes["required"] = "true";

我也在类似的控件上尝试了这种分配属性的方式:

boxRequesterName.Attributes.Add("required", "true");

我已经通过在 jQuery (*.ascx) 文件中设置的 Validate jQuery 插件进行了验证:

$(window).load(function () {
    . . .
    this.validate();
});

...在这样设置之后:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.14.0/jquery.validate.min.js" type="text/javascript"></script>

...但是当我使用该页面时不会发生验证 - 当我将 boxPayeeName 和 boxRequesterName 留空时,我看不到任何验证消息。我错过了什么?

【问题讨论】:

    标签: c# jquery validation jquery-validate required


    【解决方案1】:

    一周前我遇到了同样的问题,这是因为“必需”属性在 IE 7-9 中存在问题。

    This is because, according to the post, all versions less than IE 10 are not entirely compliant with HTML 5.

    如果您使用的是 .net(我猜您使用的是 C#),请尝试使用 RequiredFieldValidator 功能。

    【讨论】:

      猜你喜欢
      • 2019-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-09
      • 2014-01-06
      • 2021-01-23
      • 1970-01-01
      相关资源
      最近更新 更多