私は ASP.net を使用しており、次のような 3 つのテキスト ボックスと 2 つのドロップダウンがあります。
<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 を選択したときに都市のドロップダウンを検証したい。これどうやってするの?