ASP.NET验证器和jquery

时间:2011-03-03 09:49:16

标签: jquery asp.net validation

我有一个 addressControl ,其中包含多个文本框和RequiredFieldValidators。

一个页面包含2个 addressControls 和复选框。如果选中该复选框,则必须禁用第二个地址控件(正确地说 - addressControl 中的所有文本框)而不回发,反之亦然。

我需要在没有回发的情况下使用jquery来完成它。但我遇到了一些问题。一个问题是第二个地址控件上的RequiredFieldValidators变得可见,即使选中了复选框(并且禁用了地址控制)。

UPDAT2E:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head id="Head1"><title>

</title><link href="/Styles/Site.css" rel="stylesheet" type="text/css" /><link href="/Styles/jqueryui.css" rel="stylesheet" type="text/css" />
    <script   src="/Scripts/jquery/jquery-1.5.1.min.js" type="text/javascript" ></script>
    <script   src="/scripts/jquery/jquery-ui-1.8.6.custom.min.js" type="text/javascript"></script>
    <script   src="/scripts/jquery/jquery.ui.datepicker-ru.js" type="text/javascript"></script>


<script type="text/javascript">

function Validator_Enable(e, v) {

    if (v == false) {
        e.isvalid = !v; //if disabling the validator then marked it as valid.
    }
    e.enabled = v;
    //ValidatorValidate(e); DON'T DO
    ValidatorUpdateIsValid();
}

    $(document).ready(function () {
        $('#MainContent_txtRegistrationDate').datepicker();
        checkboxHandlerObj.init();
    });

    var checkboxHandlerObj =
    {
        init: function () {
            $('#customerServices input:checkbox[class="parent"]').click(checkboxHandlerObj.parentClicked);
            $('#customerServices input:checkbox[class^="parent-"]').click(checkboxHandlerObj.childClicked);
            $('#MainContent_chkLegalAddress').click(checkboxHandlerObj.chkLegalAddressClick);
        },

        parentClicked: function () {
            if ($(this).attr('checked')) {
                $('#customerServices input:checkbox[class="parent-' + $(this).attr('id') + '"]').attr('checked', 'checked');
            }
            else {
                $('#customerServices input:checkbox[class="parent-' + $(this).attr('id') + '"]').removeAttr('checked');
            }
        },

        childClicked: function () {
            var temp = $(this).attr('class').split('-');
            var parentId = temp[1];

            if ($(this).attr('checked')) {
                $('#' + parentId).attr('checked', 'checked');
            }
            else {
                var atLeastOneEnabled = false;
                $('#customerServices input:checkbox[class="' + $(this).attr('class') + '"]').each(function () {
                    if ($(this).attr('checked')) {
                        atLeastOneEnabled = true;
                    }
                });
                if (!atLeastOneEnabled) {
                    $('#' + parentId).removeAttr('checked');
                }
            }
        },

        chkLegalAddressClick: function () {
            if ($(this).is(':checked')) {
                $('#legalAddress :input').attr('disabled', true);
                var a = $('#legalAddress').find('span').attr('id');

                if (a.indexOf('RequiredFieldValidator') != -1) {

                    $('#legalAddress').find('span').each(function () {
                        if ($(this).attr("controltovalidate").length > 0) {
                            Validator_Enable(this, false);
                        }

                    })

                }
                else {
                    $('#legalAddress :input').removeAttr('disabled');
                    //$('#legalAddress').find('span').attr('style', 'visibility:visible');
                }
            }

        }
    }



</script>


</head>
<body>
    <form method="post" action="CustomerRegistration.aspx" onsubmit="javascript:return WebForm_OnSubmit();" id="Form1">
<div class="aspNetHidden">
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTc4MDEyMTgyMGQYAwUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgMFKWN0bDAwJEhlYWRMb2dpblZpZXckSGVhZExvZ2luU3RhdHVzJGN0bDAxBSljdGwwMCRIZWFkTG9naW5WaWV3JEhlYWRMb2dpblN0YXR1cyRjdGwwMwUhY3RsMDAkTWFpbkNvbnRlbnQkY2hrTGVnYWxBZGRyZXNzBRpjdGwwMCRtbnVOYXZpZ2F0aW9uVG9wTWVudQ8PZAUtQ3VzdG9tZXIn0Ytc0KDQtdCz0LjRgdGC0YDQsNGG0LjRjyBjdXN0b21lcidhZAUTY3RsMDAkSGVhZExvZ2luVmlldw8PZAIBZJWMKDc9T2zXH3REtHMTFcrLZQYH+1pHSEh2hOfM6MRL" />
</div>

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['Form1'];
if (!theForm) {
    theForm = document.Form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>



<div>

<fieldset>
    <legend>Фактический адрес клиента</legend>
<div>
<div>Страна</div>
    <span id="MainContent_ucActualAddress_RequiredFieldValidator4" style="visibility:hidden;">
            <span class="error">Введите страну</span>
    </span>

    <div><input name="ctl00$MainContent$ucActualAddress$txtCountry" type="text" id="MainContent_ucActualAddress_txtCountry" /></div>
</div>


<div>
<div>Город</div>
    <span id="MainContent_ucActualAddress_RequiredFieldValidator1" style="visibility:hidden;">
            <span class="error">Введите город</span>
    </span>
    <div><input name="ctl00$MainContent$ucActualAddress$txtCity" type="text" id="MainContent_ucActualAddress_txtCity" /></div>

</div>


<div>
<div>Улица</div>
    <span id="MainContent_ucActualAddress_RequiredFieldValidator2" style="visibility:hidden;">
            <span class="error">Введите улицу</span>
    </span>
    <div><input name="ctl00$MainContent$ucActualAddress$txtStreet" type="text" id="MainContent_ucActualAddress_txtStreet" /></div>
</div>

<div>
<div>Дом</div>
    <span id="MainContent_ucActualAddress_RequiredFieldValidator3" style="visibility:hidden;">
            <span class="error">Введите корпус</span>
    </span>
    <div> <input name="ctl00$MainContent$ucActualAddress$txtBlock" type="text" id="MainContent_ucActualAddress_txtBlock" /></div>
</div>


<div>
<div>Строение</div>
    <span id="MainContent_ucActualAddress_RequiredFieldValidator5" style="visibility:hidden;">
            <span class="error">Введите юридический адрес</span>
    </span>
    <div><input name="ctl00$MainContent$ucActualAddress$txtBuilding" type="text" id="MainContent_ucActualAddress_txtBuilding" /></div>
</div>



</fieldset></div>

<input id="MainContent_chkLegalAddress" type="checkbox" name="ctl00$MainContent$chkLegalAddress" /><label for="MainContent_chkLegalAddress">Совпадает с фактическим адресом</label>
<div id="legalAddress">


<fieldset>
    <legend>Юридический адрес клиента</legend>
<div>
<div>Страна</div>
    <span id="MainContent_ucLegalAddress_RequiredFieldValidator4" style="visibility:hidden;">
            <span class="error">Введите страну</span>

    </span>
    <div><input name="ctl00$MainContent$ucLegalAddress$txtCountry" type="text" id="MainContent_ucLegalAddress_txtCountry" /></div>
</div>


<div>
<div>Город</div>
    <span id="MainContent_ucLegalAddress_RequiredFieldValidator1" style="visibility:hidden;">
            <span class="error">Введите город</span>
    </span>

    <div><input name="ctl00$MainContent$ucLegalAddress$txtCity" type="text" id="MainContent_ucLegalAddress_txtCity" /></div>
</div>


<div>
<div>Улица</div>
    <span id="MainContent_ucLegalAddress_RequiredFieldValidator2" style="visibility:hidden;">
            <span class="error">Введите улицу</span>
    </span>
    <div><input name="ctl00$MainContent$ucLegalAddress$txtStreet" type="text" id="MainContent_ucLegalAddress_txtStreet" /></div>

</div>


<div>
<div>Дом</div>
    <span id="MainContent_ucLegalAddress_RequiredFieldValidator3" style="visibility:hidden;">
            <span class="error">Введите корпус</span>
    </span>
    <div> <input name="ctl00$MainContent$ucLegalAddress$txtBlock" type="text" id="MainContent_ucLegalAddress_txtBlock" /></div>
</div>


<div>
<div>Строение</div>
    <span id="MainContent_ucLegalAddress_RequiredFieldValidator5" style="visibility:hidden;">
            <span class="error">Введите юридический адрес</span>
    </span>
    <div><input name="ctl00$MainContent$ucLegalAddress$txtBuilding" type="text" id="MainContent_ucLegalAddress_txtBuilding" /></div>
</div>


</fieldset>
</div>






    <div>
        <input type="submit" name="ctl00$MainContent$btnSubmit" value="Сохранить" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$MainContent$btnSubmit&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" id="MainContent_btnSubmit" /> 
        <input type="submit" name="ctl00$MainContent$btnCancel" value="Отменить" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$MainContent$btnCancel&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" id="MainContent_btnCancel" />
    </div>



        </div>
        <div class="clear">

        </div>
    </div>
    <div class="footer">
         <div align="right">
            версия: 1.0.0.24109
         </div>
    </div>





<script type="text/javascript">
//<![CDATA[
var Page_Validators =  new Array(document.getElementById("MainContent_RequiredFieldValidator1"), document.getElementById("MainContent_RequiredFieldValidator2"), document.getElementById("MainContent_RequiredFieldValidator3"), document.getElementById("MainContent_ucActualAddress_RequiredFieldValidator4"), document.getElementById("MainContent_ucActualAddress_RequiredFieldValidator1"), document.getElementById("MainContent_ucActualAddress_RequiredFieldValidator2"), document.getElementById("MainContent_ucActualAddress_RequiredFieldValidator3"), document.getElementById("MainContent_ucActualAddress_RequiredFieldValidator5"), document.getElementById("MainContent_ucLegalAddress_RequiredFieldValidator4"), document.getElementById("MainContent_ucLegalAddress_RequiredFieldValidator1"), document.getElementById("MainContent_ucLegalAddress_RequiredFieldValidator2"), document.getElementById("MainContent_ucLegalAddress_RequiredFieldValidator3"), document.getElementById("MainContent_ucLegalAddress_RequiredFieldValidator5"), document.getElementById("MainContent_RequiredFieldValidator6"), document.getElementById("MainContent_RequiredFieldValidator7"), document.getElementById("MainContent_RequiredFieldValidator8"), document.getElementById("MainContent_RequiredFieldValidator9"), document.getElementById("MainContent_RequiredFieldValidator10"), document.getElementById("MainContent_RequiredFieldValidator11"), document.getElementById("MainContent_RequiredFieldValidator12"), document.getElementById("MainContent_RequiredFieldValidator13"), document.getElementById("MainContent_RequiredFieldValidator14"), document.getElementById("MainContent_RequiredFieldValidator15"), document.getElementById("MainContent_RequiredFieldValidator16"));
//]]>
</script>

<script type="text/javascript">
//<![CDATA[
var MainContent_RequiredFieldValidator1 = document.all ? document.all["MainContent_RequiredFieldValidator1"] : document.getElementById("MainContent_RequiredFieldValidator1");
MainContent_RequiredFieldValidator1.controltovalidate = "MainContent_txtShortName";
MainContent_RequiredFieldValidator1.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
MainContent_RequiredFieldValidator1.initialvalue = "";
var MainContent_RequiredFieldValidator2 = document.all ? document.all["MainContent_RequiredFieldValidator2"] : document.getElementById("MainContent_RequiredFieldValidator2");
MainContent_RequiredFieldValidator2.controltovalidate = "MainContent_txtPropertyForm";
MainContent_RequiredFieldValidator2.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
MainContent_RequiredFieldValidator2.initialvalue = "";
var MainContent_RequiredFieldValidator3 = document.all ? document.all["MainContent_RequiredFieldValidator3"] : document.getElementById("MainContent_RequiredFieldValidator3");
MainContent_RequiredFieldValidator3.controltovalidate = "MainContent_txtFullName";
MainContent_RequiredFieldValidator3.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
MainContent_RequiredFieldValidator3.initialvalue = "";
var MainContent_ucActualAddress_RequiredFieldValidator4 = document.all ? document.all["MainContent_ucActualAddress_RequiredFieldValidator4"] : document.getElementById("MainContent_ucActualAddress_RequiredFieldValidator4");
MainContent_ucActualAddress_RequiredFieldValidator4.controltovalidate = "MainContent_ucActualAddress_txtCountry";
MainContent_ucActualAddress_RequiredFieldValidator4.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
MainContent_ucActualAddress_RequiredFieldValidator4.initialvalue = "";
var MainContent_ucActualAddress_RequiredFieldValidator1 = document.all ? document.all["MainContent_ucActualAddress_RequiredFieldValidator1"] : document.getElementById("MainContent_ucActualAddress_RequiredFieldValidator1");
MainContent_ucActualAddress_RequiredFieldValidator1.controltovalidate = "MainContent_ucActualAddress_txtCity";
MainContent_ucActualAddress_RequiredFieldValidator1.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
MainContent_ucActualAddress_RequiredFieldValidator1.initialvalue = "";
var MainContent_ucActualAddress_RequiredFieldValidator2 = document.all ? document.all["MainContent_ucActualAddress_RequiredFieldValidator2"] : document.getElementById("MainContent_ucActualAddress_RequiredFieldValidator2");
MainContent_ucActualAddress_RequiredFieldValidator2.controltovalidate = "MainContent_ucActualAddress_txtStreet";
MainContent_ucActualAddress_RequiredFieldValidator2.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
MainContent_ucActualAddress_RequiredFieldValidator2.initialvalue = "";
var MainContent_ucActualAddress_RequiredFieldValidator3 = document.all ? document.all["MainContent_ucActualAddress_RequiredFieldValidator3"] : document.getElementById("MainContent_ucActualAddress_RequiredFieldValidator3");
MainContent_ucActualAddress_RequiredFieldValidator3.controltovalidate = "MainContent_ucActualAddress_txtBlock";
MainContent_ucActualAddress_RequiredFieldValidator3.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
MainContent_ucActualAddress_RequiredFieldValidator3.initialvalue = "";
var MainContent_ucActualAddress_RequiredFieldValidator5 = document.all ? document.all["MainContent_ucActualAddress_RequiredFieldValidator5"] : document.getElementById("MainContent_ucActualAddress_RequiredFieldValidator5");
MainContent_ucActualAddress_RequiredFieldValidator5.controltovalidate = "MainContent_ucActualAddress_txtBuilding";
MainContent_ucActualAddress_RequiredFieldValidator5.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
MainContent_ucActualAddress_RequiredFieldValidator5.initialvalue = "";
var MainContent_ucLegalAddress_RequiredFieldValidator4 = document.all ? document.all["MainContent_ucLegalAddress_RequiredFieldValidator4"] : document.getElementById("MainContent_ucLegalAddress_RequiredFieldValidator4");
MainContent_ucLegalAddress_RequiredFieldValidator4.controltovalidate = "MainContent_ucLegalAddress_txtCountry";
MainContent_ucLegalAddress_RequiredFieldValidator4.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
MainContent_ucLegalAddress_RequiredFieldValidator4.initialvalue = "";
var MainContent_ucLegalAddress_RequiredFieldValidator1 = document.all ? document.all["MainContent_ucLegalAddress_RequiredFieldValidator1"] : document.getElementById("MainContent_ucLegalAddress_RequiredFieldValidator1");
MainContent_ucLegalAddress_RequiredFieldValidator1.controltovalidate = "MainContent_ucLegalAddress_txtCity";
MainContent_ucLegalAddress_RequiredFieldValidator1.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
MainContent_ucLegalAddress_RequiredFieldValidator1.initialvalue = "";
var MainContent_ucLegalAddress_RequiredFieldValidator2 = document.all ? document.all["MainContent_ucLegalAddress_RequiredFieldValidator2"] : document.getElementById("MainContent_ucLegalAddress_RequiredFieldValidator2");
MainContent_ucLegalAddress_RequiredFieldValidator2.controltovalidate = "MainContent_ucLegalAddress_txtStreet";
MainContent_ucLegalAddress_RequiredFieldValidator2.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
MainContent_ucLegalAddress_RequiredFieldValidator2.initialvalue = "";
var MainContent_ucLegalAddress_RequiredFieldValidator3 = document.all ? document.all["MainContent_ucLegalAddress_RequiredFieldValidator3"] : document.getElementById("MainContent_ucLegalAddress_RequiredFieldValidator3");
MainContent_ucLegalAddress_RequiredFieldValidator3.controltovalidate = "MainContent_ucLegalAddress_txtBlock";
MainContent_ucLegalAddress_RequiredFieldValidator3.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
MainContent_ucLegalAddress_RequiredFieldValidator3.initialvalue = "";
var MainContent_ucLegalAddress_RequiredFieldValidator5 = document.all ? document.all["MainContent_ucLegalAddress_RequiredFieldValidator5"] : document.getElementById("MainContent_ucLegalAddress_RequiredFieldValidator5");
MainContent_ucLegalAddress_RequiredFieldValidator5.controltovalidate = "MainContent_ucLegalAddress_txtBuilding";
MainContent_ucLegalAddress_RequiredFieldValidator5.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
MainContent_ucLegalAddress_RequiredFieldValidator5.initialvalue = "";
var MainContent_RequiredFieldValidator6 = document.all ? document.all["MainContent_RequiredFieldValidator6"] : document.getElementById("MainContent_RequiredFieldValidator6");
MainContent_RequiredFieldValidator6.controltovalidate = "MainContent_txtActivityScope";
MainContent_RequiredFieldValidator6.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
MainContent_RequiredFieldValidator6.initialvalue = "";
var MainContent_RequiredFieldValidator7 = document.all ? document.all["MainContent_RequiredFieldValidator7"] : document.getElementById("MainContent_RequiredFieldValidator7");
MainContent_RequiredFieldValidator7.controltovalidate = "MainContent_txtPhone";
MainContent_RequiredFieldValidator7.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
MainContent_RequiredFieldValidator7.initialvalue = "";
var MainContent_RequiredFieldValidator8 = document.all ? document.all["MainContent_RequiredFieldValidator8"] : document.getElementById("MainContent_RequiredFieldValidator8");
MainContent_RequiredFieldValidator8.controltovalidate = "MainContent_txtFax";
MainContent_RequiredFieldValidator8.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
MainContent_RequiredFieldValidator8.initialvalue = "";
var MainContent_RequiredFieldValidator9 = document.all ? document.all["MainContent_RequiredFieldValidator9"] : document.getElementById("MainContent_RequiredFieldValidator9");
MainContent_RequiredFieldValidator9.controltovalidate = "MainContent_txtINN";
MainContent_RequiredFieldValidator9.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
MainContent_RequiredFieldValidator9.initialvalue = "";
var MainContent_RequiredFieldValidator10 = document.all ? document.all["MainContent_RequiredFieldValidator10"] : document.getElementById("MainContent_RequiredFieldValidator10");
MainContent_RequiredFieldValidator10.controltovalidate = "MainContent_txtKPP";
MainContent_RequiredFieldValidator10.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
MainContent_RequiredFieldValidator10.initialvalue = "";
var MainContent_RequiredFieldValidator11 = document.all ? document.all["MainContent_RequiredFieldValidator11"] : document.getElementById("MainContent_RequiredFieldValidator11");
MainContent_RequiredFieldValidator11.controltovalidate = "MainContent_txtSettlementAccount";
MainContent_RequiredFieldValidator11.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
MainContent_RequiredFieldValidator11.initialvalue = "";
var MainContent_RequiredFieldValidator12 = document.all ? document.all["MainContent_RequiredFieldValidator12"] : document.getElementById("MainContent_RequiredFieldValidator12");
MainContent_RequiredFieldValidator12.controltovalidate = "MainContent_txtCorrespondentAccount";
MainContent_RequiredFieldValidator12.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
MainContent_RequiredFieldValidator12.initialvalue = "";
var MainContent_RequiredFieldValidator13 = document.all ? document.all["MainContent_RequiredFieldValidator13"] : document.getElementById("MainContent_RequiredFieldValidator13");
MainContent_RequiredFieldValidator13.controltovalidate = "MainContent_txtBankName";
MainContent_RequiredFieldValidator13.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
MainContent_RequiredFieldValidator13.initialvalue = "";
var MainContent_RequiredFieldValidator14 = document.all ? document.all["MainContent_RequiredFieldValidator14"] : document.getElementById("MainContent_RequiredFieldValidator14");
MainContent_RequiredFieldValidator14.controltovalidate = "MainContent_txtBIK";
MainContent_RequiredFieldValidator14.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
MainContent_RequiredFieldValidator14.initialvalue = "";
var MainContent_RequiredFieldValidator15 = document.all ? document.all["MainContent_RequiredFieldValidator15"] : document.getElementById("MainContent_RequiredFieldValidator15");
MainContent_RequiredFieldValidator15.controltovalidate = "MainContent_txtBOSSName";
MainContent_RequiredFieldValidator15.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
MainContent_RequiredFieldValidator15.initialvalue = "";
var MainContent_RequiredFieldValidator16 = document.all ? document.all["MainContent_RequiredFieldValidator16"] : document.getElementById("MainContent_RequiredFieldValidator16");
MainContent_RequiredFieldValidator16.controltovalidate = "MainContent_txtRegistrationDate";
MainContent_RequiredFieldValidator16.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
MainContent_RequiredFieldValidator16.initialvalue = "";
//]]>
</script>

<script type='text/javascript'>new Sys.WebForms.Menu({ element: 'mnuNavigationTopMenu', disappearAfter: 500, orientation: 'horizontal', tabIndex: 0, disabled: false });</script>
<script type="text/javascript">
//<![CDATA[

var Page_ValidationActive = false;
if (typeof(ValidatorOnLoad) == "function") {
    ValidatorOnLoad();
}

function ValidatorOnSubmit() {
    if (Page_ValidationActive) {
        return ValidatorCommonOnSubmit();
    }
    else {
        return true;
    }
}
        //]]>
</script>
</form>
</body>
</html>

3 个答案:

答案 0 :(得分:1)

客户端在您的情况下禁用验证器可能会非常棘手。按以下方式执行

//e is the validator and v is true or false
function Validator_Enable(e, v) {

    if (v == false) {
        e.isvalid = !v; //if disabling the validator then marked it as valid.
    }
    e.enabled = v;
    //ValidatorValidate(e); DON'T DO
    ValidatorUpdateIsValid();
}


chkLegalAddressClick: function () {
            if ($(this).attr("checked")) {
                $('#legalAddress :input').attr('disabled', true);
                var a = $('#legalAddress').find('span').attr('id');
                if (a.indexOf('RequiredFieldValidator') != -1){
                    //a.attr('style', 'visibility:hidden');
                }
                $(Page_Validators).each(function(){
                      if ($(this).attr("id").indexOf("ucLegalAddress_RequiredFieldValidator")>-1)
                               Validator_Enable(this,false);
                });
            }
            else {
                $('#legalAddress :input').removeAttr('disabled');
                //$('#legalAddress').find('span').attr('style', 'visibility:visible');
            }

我的代码中的技巧是Validator_Enable函数。您还可以使用for循环遍历客户端上的Page_Validators数组,查看每个验证程序的“controltovalidate”属性,以查看它是否是要禁用的验证程序并将其传递给Validator_Enable函数。

如果我的回答对您有帮助,请选中旁边的复选框接受并投票!

答案 1 :(得分:1)

如果将jquery代码重复到此函数中该怎么办?

 var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_endRequest(function () {

所以:

<script type="text/javascript">

$(document).ready(function () {
    checkboxHandlerObj.init();
});

var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm.add_endRequest(function () {
checkboxHandlerObj.init();
});

var checkboxHandlerObj =
{
    init: function () {
       $('#legalAddress  input:checkbox').click(checkboxHandlerObj.chkLegalAddressClick);
    },


    chkLegalAddressClick: function () {
        if ($(this).is(':checked')) {
            $('#legalAddress :input').attr('disabled', true);
            var a = $('#legalAddress').find('span').attr('id');
            if (a.indexOf('RequiredFieldValidator') != -1) {

                this.isvalid = true; //if disabling the validator then marked it as valid.
                this.enabled = false; 
                ValidatorUpdateIsValid();

            }
        }
        else {
            $('#legalAddress :input').removeAttr('disabled');

        }
    }

}

我不确定它会起作用,但你可以尝试一下!

答案 2 :(得分:0)

你可能会发现我写的有用的验证器。它允许您指定一个控件(通常是一个复选框)作为确定是否需要TextBox的条件。您可以自由修改代码,它可以在我的页面here上找到。