我正在使用 ASP.net,我有三个文本框和两个下拉列表,如下所示:
<div style="float: left; width: 100%;">
<div style="float: left; width: 30%;">
<asp:Label ID="FullNameLabel" runat="server" Text="Full Name : "></asp:Label></div>
<div class="inputDiv">
<asp:TextBox ID="FullNameTextBox" runat="server" CssClass="smallTxtBox">
</asp:TextBox></div>
</div>
<div class="formlinebreak">
</div>
<div style="float: left; width: 100%;">
<div style="float: left; width: 30%;">
<asp:Label ID="StateNameLabelLabel" runat="server" Text="State Name"></asp:Label></div>
<div class="inputDiv">
<asp:DropDownList ID="StateDropDownList" runat="server" AppendDataBoundItems="True"
ValidationGroup="rfvStepOneBasicInput" OnSelectedIndexChanged="StateDropDownList_SelectedIndexChanged"
AutoPostBack="true" CssClass="dropDown">
</asp:DropDownList>
</div>
</div>
<div class="formlinebreak">
</div>
<div style="float: left; width: 100%;">
<div style="float: left; width: 30%;">
<asp:Label ID="CityNameLabel" runat="server" Text="City Name"></asp:Label></div>
<div class="inputDiv">
<asp:UpdatePanel ID="CityUpdatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList ID="CityDropDownList" runat="server" AppendDataBoundItems="True"
ValidationGroup="rfvStepOneBasicInput" CssClass="dropDown">
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="StateDropDownList" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</div>
</div>
<div class="formlinebreak">
</div>
<div style="float: left; width: 100%;">
<div style="float: left; width: 30%;">
<asp:Label ID="FeedBackLabel" runat="server" Text="Your FeedBack"></asp:Label></div>
<div style="float: left; width: 60%;">
<asp:TextBox ID="FeedBackTextBox" runat="server" CssClass="multilineTxtBox" TextMode="MultiLine"></asp:TextBox></div>
</div>
<div class="formlinebreak">
</div>
<asp:Panel ID="GeneralUserPanel" runat="server" Visible="true">
<div style="float: left; width: 100%;">
<div style="float: left; width: 30%;">
<asp:Label ID="Label2" runat="server" Text="Your Email ID"></asp:Label></div>
<div style="float: left; width: 60%;">
<asp:TextBox ID="GeneralEmailIDTextBox" runat="server" CssClass="smallTxtBox"></asp:TextBox></div>
</div>
<div class="formlinebreak">
</div>
</asp:Panel>
我正在验证来自 jQuery 的这些输入,如下所示:
$("#aspnetForm").validate({
rules: {
'<%=FullNameTextBox.UniqueID %>': {
required: true
},
'<%=StateDropDownList.UniqueID %>': {
selectNone: true,
required: true
},
'<%=CityDropDownList.UniqueID %>': {
selectNone: true,
required: true
},
'<%=FeedBackTextBox.UniqueID %>': {
required: true
},
'<%=GeneralEmailIDTextBox.UniqueID %>': {
required: true
}
},
messages: {
'<%=FullNameTextBox.UniqueID %>': {
required: "<span style='color:#F87126;padding-left:10px; font-size:smaller;'>Full name is required. </span>"
},
'<%=StateDropDownList.UniqueID %>': {
selectNone: "<span style='color:#F87126;padding-left:10px; font-size:smaller;'>State is required. </span>",
required: "<span style='color:#F87126;padding-left:10px; font-size:smaller;'>State is required. </span>"
},
'<%=CityDropDownList.UniqueID %>': {
selectNone: "<span style='color:#F87126;padding-left:10px; font-size:smaller;'>City is required. </span>",
required: "<span style='color:#F87126;padding-left:10px; font-size:smaller;'>City is required. </span>"
},
'<%=FeedBackTextBox.UniqueID %>': {
required: "<span style='color:#F87126;padding-left:10px; font-size:smaller;'>Feedback is required. </span>"
},
'<%=GeneralEmailIDTextBox.UniqueID %>': {
required: "<span style='color:#F87126;padding-left:10px; font-size:smaller;'>Emil-ID is required. </span>"
}
}
});
但我只想在州 ID 为 1 时验证城市。我尝试了以下代码document.ready
:
if ($('[id$=StateDropDownList]').val() == "1") {
$("#aspnetForm").validate({ //Imp #aspnetForm is the ID of the <form> in site.Master
rules: {
'<%=CityDropDownList.UniqueID %>': {
required: true,
selectNone: true
}
},
messages:{
'<%=CityDropDownList.UniqueID %>': {
selectNone: "<span style='color:#F87126;padding-left:10px; font-size:smaller;'>City is required. </span>",
required: "<span style='color:#F87126;padding-left:10px; font-size:smaller;'>City is required. </span>"
}
});
}
但我什至无法验证。我想同时验证除城市下拉列表之外的所有我想在用户选择 stateid=1 时验证城市下拉列表。我怎样才能做到这一点?