如何不触发的RequiredFieldValidator的隐藏字段(文本框) [英] How not to trigger RequiredFieldValidator on hidden field (TextBox)
问题描述
我觉得这是一个常见的问题。我有一个表格,我显示/隐藏域动态使用jQuery,这取决于一些单选按钮。
I think this is a common problem. I have a form where I show/hide fields dynamically using jQuery, depending on some radio buttons.
我对所有字段的RequiredFieldValidator的,但我不想,如果他们的ControlToValidate是隐藏的(使用jQuery)他们被触发。
I have RequiredFieldValidator's on all the fields, but I don't want them to be triggered if their ControlToValidate is hidden (using jQuery).
这可能吗?先谢谢了。
编辑:这里是解决方案,感谢马雷克。如果你有因为MasterPages怪异的ClientID它可能不是很明显。
Here is the solution, thanks to Marek. It might not be very obvious if you have weird clientIDs because of MasterPages
这是ASPX
<asp:TextBox ID="txtName" runat="server" />
<asp:RequiredFieldValidator ID="vldName" ControlToValidate="txtName" runat="server" ErrorMessage="You must enter Name!" />
...
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
这是jQuery的
$(function() {
$('#ctl00_cphContent_btnSubmit').click(function() {
if (!$('#ctl00_cphContent_txtName').is(':visible'))
ValidatorEnable(ctl00_cphContent_vldName, false);
});
});
希望它会使人的生活更轻松。
Hope it will make someone's life easier
推荐答案
如果我没有记错,有一个名为 ValidatorEnable(validatorClientId,isEnabled)
功能,可以禁用/能够通过JavaScript的ASP.NET验证。你可以使用jQuery权在你的表单提交禁用所有无形的验证。
If I remember correctly there's a function called ValidatorEnable(validatorClientId, isEnabled)
that allows you to disable/enable the ASP.NET validators via javascript. You could use jQuery right before your form submit to disable all your invisible validators.
有是关于客户端API提供一些文件从这里验证的http:// msdn.microsoft.com/en-us/library/aa479045.aspx
There's some documentation about client side API available from the validators here http://msdn.microsoft.com/en-us/library/aa479045.aspx
这篇关于如何不触发的RequiredFieldValidator的隐藏字段(文本框)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!