根據用戶在完成表單時的反應,控件可能需要也可能不需要。所有這些控件都附帶了驗證。例如,如果用戶至少沒有住在他們的地址2年,我需要驗證以前的地址信息;如果不是,我需要跳過驗證。禁止在asp.net控件上驗證
我試着禁用驗證控件與jQuery,雖然當驗證文本是'變灰',它仍然會導致驗證。
<script src="../../Scripts/jquery-1.6.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#ddlSpouseData').change(
function() {
var selection = $('#<%= ddlAddressData.ClientID%>').get(0).selectedIndex;
if (selection == 1) {
alert(selection);
$('#rfvAddressType').attr('disabled', 'disabled');
$('#rfvBAddress').attr('disabled', 'disabled');
}
});
});
</script>
<table id="MyTable" class="tableModule">
<tr>
<td class="tableDataBorder">
Data is not available and<br /> will be provided later.
</td>
<td class="tableDataBorder">
<asp:DropDownList ID="ddlAddressData" runat="server">
<asp:ListItem>Please Select</asp:ListItem>
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr class="hideRow">
<td class="tableDataBorder">
Identifying Document
</td>
<td class="tableDataBorder">
<asp:DropDownList ID="ddlAddressType" runat="server">
<asp:ListItem>Please Select</asp:ListItem>
<asp:ListItem>Home</asp:ListItem>
<asp:ListItem>Office</asp:ListItem>
<asp:ListItem>Other</asp:ListItem>
</asp:DropDownList>
<br />
<asp:RequiredFieldValidator ID="rfvAddressType" CssClass="regexError" ControlToValidate="ddlAddressType"
runat="server" ErrorMessage="Please select address type" Display="Dynamic"
InitialValue="Please Select"></asp:RequiredFieldValidator>
</td>
</tr>
<tr class="hideRow">
<td class="tableDataBorder">
Address
</td>
<td>
<asp:TextBox ID="txtAddress" MaxLength="50" runat="server" CssClass="StdTxtBox"></asp:TextBox><br />
<asp:RequiredFieldValidator ID="rfvBAddress" CssClass="regexError" ControlToValidate="txtAddress"
runat="server" ErrorMessage="Please enter your address" Display="Dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
</table>
也許回答這個問題以前將幫助。 http://stackoverflow.com/questions/2379445/enabling-disabling-asp-net-validation-controls-using-jquery – SeaSharp 2012-08-01 21:23:06