如何在隐藏时从 ASP.NET 验证中排除控件?
复选框用于在两个文本框之间切换。
其中一个是必需的不是两个。
我希望必填字段在隐藏时禁用。
我知道 enabled="false"
和 visible="false"
创建了这种行为,但是我想保持 jquery 淡入淡出动画。
我试图将 disabled false 属性添加到 javascript 但无法让它工作。 在线找到的其他解决方案似乎无法实现所需的功能。
ASP.NET
<div class="Hide1">
<asp:TextBox ID="tb" runat="Server"/>
<asp:RequiredFieldValidator ID="rfv1" RunAt="Server" ControlToValidate="tb"/>
</div>
<div class="Hide2">
<asp:TextBox ID="tb2" runat="Server"/>
<asp:RequiredFieldValidator ID="rfv2" RunAt="Server" ControlToValidate="tb2"/>
</div>
<asp:Button ID="btn" runat="Server" Text="GO"/>
JQUERY
$('.tb').hide();
$('#CB').change(function () {
if ($(this).is(':checked')) {
$('.tb2').fadeOut(100, function () {
$('.tb').fadeIn();
});
} else {
$('.tb').fadeOut(100, function () {
$('.tb2').fadeIn();
});
}
});
最佳答案
你可以这样试试
if (document.getElementById('<%=CB.ClientID%>').checked) {
ValidatorEnable(document.getElementById('<%= rfv1.ClientID %>'), true);
ValidatorEnable(document.getElementById('<%= rfv2.ClientID %>'), false);
$('.tb2').fadeOut(100, function () {
$('.tb').fadeIn();
});
}
else {
ValidatorEnable(document.getElementById('<%= rfv1.ClientID %>'), false);
ValidatorEnable(document.getElementById('<%= rfv2.ClientID %>'), true);
$('.tb').fadeOut(100, function () {
$('.tb2').fadeIn();
});
}
注意:rfv1 和 rfv2 是两个必需的验证器的 ID,请避免为验证器提供相同的 ID。
更新
if (document.getElementById('<%=CB.ClientID%>').checked) {
ValidatorEnable(document.getElementById("#<%= rfv1.ClientID %>"), true);
ValidatorEnable(document.getElementById("#<%= rfv2.ClientID %>"), false);
$('.tb2').fadeOut(100, function () {
$('.tb').fadeIn();
});
}
else {
ValidatorEnable(document.getElementById("#<%= rfv1.ClientID %>"), false);
ValidatorEnable(document.getElementById("#<%= rfv2.ClientID %>"), true);
$('.tb').fadeOut(100, function () {
$('.tb2').fadeIn();
});
}
关于javascript - 如何在隐藏时从 ASP.NET 验证中排除控件(显示 :none;)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23953017/