单选按钮选择的主动必需验证

时间:2017-06-09 19:57:32

标签: jquery asp.net webforms

我想激活/禁用下拉列表中的必填字段,如果radiobutton1选中requiredvalidator,则其他地方保持禁用状态。

html代码

 <asp:RadioButton runat="server" id="radioA1" GroupName="overseas" /> 
  <label for="radioA1">Yes </label> 

  <asp:RadioButton runat="server" id="radioA2" GroupName="overseas" /> 
  <label for="radioA2">No</label>

  <asp:RadioButton runat="server" id="radioA3" GroupName="overseas" /> 
  <label for="radioA2">Not Applicable</label>
  <br />
  Overseas Country 
  <asp:DropDownList ID="DropDownList10" AppendDataBoundItems="True" CssClass="form-control mySelecBox" runat="server" DataSourceID="newCountry" DataTextField="Country" DataValueField="CountryGuid">
          <asp:ListItem Text="" Value="" />                              
  </asp:DropDownList>

<asp:SqlDataSource ID="newCountry" runat="server" ConnectionString="<%$ ConnectionStrings:umtonlineConnectionString %>" SelectCommand="SELECT [CountryGuid], [Country] FROM [countries]"></asp:SqlDataSource>

<asp:RequiredFieldValidator ID="RequiredFieldValidator13" runat="server" ControlToValidate="DropDownList10" Enabled="False" ErrorMessage="Oversea country required *" ForeColor="#CC0000" ValidationGroup="step1"></asp:RequiredFieldValidator>

                        <br />

Jquery代码:

 $(document).ready(function () {

        $('#<%=radioA1.ClientID%>').click(function () {

            alert("its working");     // this part work fine.

            ValidatorEnable($('#<%=RequiredFieldValidator13%>'));

        });
     });               

1 个答案:

答案 0 :(得分:1)

首先,显然应该调用ClientID:

'#<%=RequiredFieldValidator13.ClientID%>'

其次,ValidatorEnable需要一个dom对象,而不是jQuery对象。所以

var validator = document.getElementById('<%=RequiredFieldValidator13.ClientID%>');

或者,正如kman在评论中所建议的那样:

var validator = $('#<%=RequiredFieldValidator13.ClientID%>')[0];

第三,它还需要第二个参数boolean,指定是启用还是禁用验证器。我想这应该根据下拉列表值设置。