我正在使用JQuery隱藏/顯示一個div,具體取決於組合框的選定值。這部分工作正常。但是,當隱藏div時,jquery函數也應該禁用那個div中的3個RequiredFieldValidators。我一直在尋找網上,似乎這可以很容易地使用來完成:使用JQuery來禁用RequiredFieldValidators
ValidatorEnable(ValidatorName, false);
但是當我嘗試使用這種方法,實在不行,在RequiredFieldValidators仍然顯示錯誤,即使DIV是隱藏的。
我的jQuery函數:
<script type="text/javascript">
$(document).ready(function() {
var det = $("#SponsorDetails");
$(det).hide();
var all = $("#AllDetails");
$(all).hide();
$("#<%=SelectAccount.ClientID %>").click(function() {
//hide social worker and sponsor stuff
var value = $("#<%=SelectAccount.ClientID %> option:selected").val();
if (value == "Social_Worker") {
//show social worker stuff
$("#AllDetails").show("slow");
$("#SponsorDetails").hide("slow");
ValidatorEnable(document.getElementById("#<%=AddressValidator.ClientID %>"), false);
ValidatorEnable(document.getElementById("#<%=CityValidator.ClientID %>"), false);
ValidatorEnable(document.getElementById("#<%=CountryValidator.ClientID %>"), false);
} else if (value == "Sponsor") {
//show sponsor stuff
$("#AllDetails").show("slow");
$("#SponsorDetails").show("slow");
ValidatorEnable(document.getElementById("#<%=AddressValidator.ClientID %>"), true);
ValidatorEnable(document.getElementById("#<%=CityValidator.ClientID %>"), true);
ValidatorEnable(document.getElementById("#<%=CountryValidator.ClientID %>"), true);
}
});
});
</script>
有人建議我可以使用驗證組或自定義的驗證,但使用jQuery似乎只是簡單得多,但我不知道爲什麼它不會工作。
感謝,它的工作! – Matt 2012-03-06 11:03:31