简体   繁体   English

jQuery-获取.NET的值RequiredFieldValidator“ ControlToValidate”

[英]jQuery - Get value of .NET RequiredFieldValidator “ControlToValidate”

I am trying to create a simple function that will loop through all requiredfield elements with the ".err" class. 我正在尝试创建一个简单的函数,该函数将使用“ .err”类遍历所有必填字段。 When I catch such an element I want to add some css classes to its associated ControlToValidate. 当我捕获到这样的元素时,我想向其关联的ControlToValidate添加一些CSS类。 However, I keep getting "undefined" as a result. 但是,结果一直是“未定义”。 Below is a sample: 下面是一个示例:

Markup: 标记:

<asp:TextBox ID="txtbx" runat="server" />
<asp:RequiredFieldValidator ID="rfv" ClientIDMode="Static" CssClass="err" runat="server" ControlToValidate="txtbx" />

js JS

$("#btn").click(function () {
    $(".err").each(function (j) {
        if ($(this).css("visibility") !== "hidden") {
            console.log($(this).attr("ControlToValidate")); //produces undefined
            console.log($(this).attr("id"); //logs correct id
        }
    });
});

I have also tried val.("ControlToValidate") which produced undefined as well. 我也尝试了val.("ControlToValidate")产生未定义的。

I feel like the solution is right under my nose. 我觉得解决方案就在我的鼻子底下。 Please help! 请帮忙!

You could name the RequiredFieldValidator and append the name of the TextBox to it. 您可以命名RequiredFieldValidator并将TextBox的名称附加到它的后面。

<asp:RequiredFieldValidator ID="rfv_txtbx" ClientIDMode="Static" CssClass="err" runat="server" ControlToValidate="txtbx" />

Or you could look into Microsoft's page validators which should give you access to controltovalidate : http://msdn.microsoft.com/en-us/library/aa479045.aspx 或者,您可以查看Microsoft的页面验证器,它应使您可以访问controltovalidate: http : //msdn.microsoft.com/zh-cn/library/aa479045.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM