3

我有 2 个字段需要验证,如果它们显示在屏幕上。当表单最初加载时,它们是隐藏的,除非从下拉框中选择项目,否则它们将保持隐藏状态。选择值后,将出现 2 个字段,然后验证正常工作。但是,如果选择了另一个不会使这两个字段出现的值,则它们仍在验证中并且不允许页面提交。关于如何实现这一目标的任何想法?

function DisplayOutageDates() {
    if ($('#ddImpact').val() == "Service Affecting") { 
    $('#outageDates').css('display',''); 
    document.getElementById('txtOutageStartDate').Visible = true;
    document.getElementById('RFVOutageStartDate').Visible = true;
    } else {
    $('#outageDates').css('display','none');
    document.getElementById('txtOutageStartDate').Visible = false;
    document.getElementById('RFVOutageStartDate').Visible = false;
    }
}

<asp:RequiredFieldValidator ID="RFVOutageStartDate" runat="server" 
    ControlToValidate="txtOutageStartDate" SetFocusOnError="true"
    ErrorMessage="Please enter the Outage Start Date" />
4

2 回答 2

2

您可以使用 :

ValidatorEnable(val, enable): 
Takes a client-validator and a Boolean value. 
Enables or disables a client validator. 
Being disabled will stop it from evaluating and it will always appear valid. 

在msdn上找到。

使用 Javascript 这看起来像:

ValidatorEnable(document.getElementById('<%=Validator1.ClientID%>'), state);
//where state would be a boolean 

在 JQuery 中,这看起来像:

 ValidatorEnable($("#<%= Validator1.ClientID %>")[0], state);

在这里找到:http: //codeclimber.net.nz/archive/2008/05/14/how-to-manage-asp.net-validation-from-javascript-with-jquery.aspx

于 2013-02-14T12:09:15.420 回答
1

我猜你需要显示和隐藏Validator控件作为显示和隐藏输入控件。


更新

如果您Validator使用它们隐藏控件,display:none;它们仍然会被渲染并参与验证过程。您需要通过将Visible属性设置为false来隐藏它们。这样他们就不会被渲染,也不会参与验证过程。

于 2013-02-14T11:06:27.317 回答