在jquery中点击按钮启用特定的验证器

时间:2018-07-04 11:41:39

标签: javascript jquery asp.net

我正在尝试禁用文本框的验证器。但在按钮中单击其触发

JS:

$("#content_rbOneYear").click(function() {
  debugger;
  var test = document.getElementById('<%= txtOneYear.ClientID %>');
  var test2 = document.getElementById('<%= txtTwoYear.ClientID %>');
  ValidatorEnable(test, true);
  ValidatorEnable(test2, true);
  test.style.visibility = "visible";
  test2.style.visibility = "visible";
  $("#content_txtOneYear").prop('disabled', false);
  $("#content_txtTwoYear").prop('disabled', true);
});
$("#content_rbTwoYear").click(function() {
  debugger;
  var test = document.getElementById('<%= txtOneYear.ClientID %>');
  var test2 = document.getElementById('<%= txtTwoYear.ClientID %>');
  ValidatorEnable(test, false);
  ValidatorEnable(test2, true);
  test.style.visibility = "visible";
  test2.style.visibility = "visible";
  $("#content_txtOneYear").prop('disabled', true);
  $("#content_txtTwoYear").prop('disabled', false);
});

HTML:

<tr>
  <th style="font-weight: normal">
    <asp:RadioButton ID="rbOneYear" Style="margin-right: 10px;" runat="server" Text="1 Year" GroupName="measurementSystem"></asp:RadioButton>
  </th>
  <th style="font-weight: normal">
    <asp:TextBox ID="txtOneYear" CssClass="clsPackage" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="validator1" ControlToValidate="txtOneYear" runat="server" ErrorMessage="*" ForeColor="Red" ValidationGroup="Group1" />
  </th>

</tr>
<tr>
  <th style="font-weight: normal">
    <asp:RadioButton ID="rbTwoYear" Style="margin-right: 10px;" Text="2 Year" runat="server" GroupName="measurementSystem"></asp:RadioButton>
  </th>
  <th style="font-weight: normal">
    <asp:TextBox ID="txtTwoYear" CssClass="clsPackage" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="validator2" ControlToValidate="txtTwoYear" runat="server" ErrorMessage="*" ForeColor="Red" ValidationGroup="Group1" />
  </th>

</tr>

在按钮中,单击“我将验证组指定为Group1”。在两个文本框中都显示错误消息

1 个答案:

答案 0 :(得分:0)

您应该传递验证器的clientID而不是控件:                     var test = document.getElementById('<%= validator1.ClientID%>');                     var test2 = document.getElementById('<%= validator2.ClientID%>');                     ValidatorEnable(test,false);                     ValidatorEnable(test2,true);