启用/禁用ASP:使用jQuery验证 [英] Enable/Disable asp:validators using jquery

查看:157
本文介绍了启用/禁用ASP:使用jQuery验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用向导,用户可以注册工作。有一个ASP:RadioButtonList的两个选项,有的在向导中输入字段改变了单选按钮更改时。每个字段有一些的asp:校验器(ASP:的RequiredFieldValidator例如)。问题是,当用户提交页面,对于隐藏的文本框验证器仍然弹出。

I am working with a wizard, where the user can sign up. There is a asp:RadioButtonList with two options, and some of the input fields in the wizard changes when the radiobutton changes. On each field there is some asp:Validators (asp:RequiredFieldValidator for example). The problem is, that when the user submits the page, the validator for the hidden textbox is still popping up.

首先,这里是div标签而改变显示文本框,并在RadioButtonList

First, here is the div tags which changes the shown textboxes and the RadioButtonList

<div id="divTxt1">
  <asp:TextBox runat="server" CssClass="text" ID="txtNumber"
       type="number"/>
  <asp:RequiredFieldValidator ID="RequiredFieldValidator1" 
       runat="server" ControlToValidate="txtNumber" EnableClientScript="true" ErrorMessage="Error" ToolTip="Error">*
   </asp:RequiredFieldValidator>
</div>
<div id="divTxt2">
  <asp:TextBox runat="server" CssClass="text" ID="txtNumber2"
       type="number"/>
  <asp:RequiredFieldValidator ID="RequiredFieldValidator2" 
       runat="server" ControlToValidate="txtNumber2" EnableClientScript="true" ErrorMessage="Error2" ToolTip="Error2">*
   </asp:RequiredFieldValidator>
</div>
<div id="radio">
<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal">
   <asp:ListItem Value="1" Selected="True">Privat</asp:ListItem>
   <asp:ListItem Value="2">Offentlig</asp:ListItem>
   </asp:RadioButtonList>
</div>

我曾尝试使用JQuery像下面这样,我已经读了应该做的伎俩来解决它,但不幸的是它不会:

I have tried to solve it using JQuery like the following, which I have read should do the trick, but unfortunately it doesn't:

$(document).ready(function () {

    $('#<%= WizardStep1.ContentTemplateContainer.FindControl("RadioButtonList1").ClientID %> input').change(function () {
        if ($(this).val() == "1") {
            $('#txtNumber').toggle('fast');
            $('#txtNumber2').toggle('fast');     
            ValidatorEnable($('#<%=WizardStep1.ContentTemplateContainer.FindControl("RequiredFieldValidator1").ClientID %>')[0], false);
            ValidatorEnable($('#<%=WizardStep1.ContentTemplateContainer.FindControl("RequiredFieldValidator2").ClientID %>')[0], true);
        }

        if ($(this).val() == "2") {
            $('#txtNumber').toggle('fast');
            $('#txtNumber2').toggle('fast');
            ValidatorEnable($('#<%=WizardStep1.ContentTemplateContainer.FindControl("RequiredFieldValidator2").ClientID %>')[0], false);
            ValidatorEnable($('#<%=WizardStep1.ContentTemplateContainer.FindControl("RequiredFieldValidator1").ClientID %>')[0], true);
        }
    });
});

所以,任何想法有什么不对?

So, any ideas what's wrong?

推荐答案

客户端API用于验证的的>。

The client side API for validators is here.

有些事情,你也许能够适应您的需求(这将停用通过客户端脚本的所有验证):

Something you may be able to adapt to your needs (this will disable all the validators via client script):

if (Page_Validators) {
    PageValidators.forEach(function(pageValidator) {
        if (pageValidator == null) {return;}
        vldGrp = pageValidator.validationGroup;
        ValidatorEnable(pageValidator, false);
    });
};

所以,你可以添加如果块检查校验器名,或更使 .controlToValidate 返回验证器的目标ID - 然后将其禁用:

So you could add a if block to check the validator name, or more so the .controlToValidate which returns the target ID of the validator - then disable it:

if (Page_Validators) {
    PageValidators.forEach(function(pageValidator) {
        if (pageValidator == null) {return;}
        if (pageValidator.controltovaliddate != "<%= txtNumber2.ClientID %>") {
            return;
        }
        ValidatorEnable(pageValidator, false);
    });
};


您应该也可能是在循环添加break如果它是正确的,如果你不需要检查任何进一步的验证。您可以使用<一个href=\"http://stackoverflow.com/questions/2641347/how-to-short-circuit-array-foreach-like-calling-break\"><$c$c>.some而不是 .forEach 打破早期:


You should also probably add a break in the loop if it's right one, if you don't need to check any further validators. You can use .some instead of .forEach to break early:

if (Page_Validators) {
    PageValidators.some(function(pageValidator) {
        if (pageValidator == null) {return false;}
        if (pageValidator.controltovaliddate != "<%= txtNumber2.ClientID %>") {
            return false;
        }
        ValidatorEnable(pageValidator, false);
        return true;
    });
};


您可以封装成函数如下:


You can encapsulate this into a function:

var validatorState = function(element, isEnabled) {
    if (Page_Validators) {
        PageValidators.some(function(pageValidator) {
            if (pageValidator == null) {return false;}
            if (pageValidator.controltovaliddate != "<%= txtNumber2.ClientID %>") {
                return false;
            }
            ValidatorEnable(pageValidator, false);
            return true;
        });
    };
};

和使用:

validatorState('txtCancellationReson', true);

validatorState($('#txtCancellationReson').attr('id'), true);

这篇关于启用/禁用ASP:使用jQuery验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆