简体   繁体   English

jQuery处理ASP.NET RadioButtonList的客户端事件

[英]jQuery handle client side event of ASP.NET RadioButtonList

I want to run some Javascript code when the selected value of an ASP.Net RadioButtonList is changed, using a jQuery event handler; 当使用jQuery事件处理程序更改ASP.Net RadioButtonList的选定值时,我想运行一些Javascript代码。 specifically I want to enable/disable a RequiredFieldValidator on a TextBox (depending on the selected value of the RadioButtonList).. But when I click one of the items in the RadioButtonList (changing its value), nothing is happening. 具体来说,我想在TextBox上启用/禁用RequiredFieldValidator(取决于RadioButtonList的选定值)。但是,当我单击RadioButtonList中的一项(更改其值)时,没有任何反应。

Here's the Javascript code: 这是Javascript代码:

var rbAuth = "<%= rbServiceHours.ClientID %>";
var vRespName = "<%= vRespNameReq.ClientID %>";
var vRespTitle = "<%= vRespTitleReq.ClientID %>";
var tbxRespName = "<%= tbxRespName.ClientID %>";
var tbxRespTitle = "<%= tbxRespTitle.ClientID %>";
$('#' + rbAuth + " > input").click(function () {
  if (($('#' + rbAuth).find('input:checked').val()) == 'Yes') {
    $('#' + vRespName).attr('enabled', 'true');
    $('#' + vRespTitle).attr('enabled', 'true');
    $('#' + tbxRespName).addClass("required");
    $('#' + tbxRespTitle).addClass("required");
  }
  else {
    $('#' + vRespName).attr('enabled', 'false');
    $('#' + vRespTitle).attr('enabled', 'false');
    $('#' + tbxRespName).removeClass("required");
    $('#' + tbxRespTitle).removeClass("required");
  }
});

This is, of course, inside $(document).ready( function() {}); 当然,这是在$(document).ready( function() {});

Here's the corresponding ASP.Net markup: 这是相应的ASP.Net标记:

<asp:RadioButtonList ID="rbServiceHours" runat="server" RepeatDirection="Horizontal"
  CssClass="required" ValidationGroup="valIncident" TabIndex="31">
    <asp:ListItem Value="Y" Text="Yes"></asp:ListItem>
    <asp:ListItem Value="N" Text="No"></asp:ListItem>
</asp:RadioButtonList>

<asp:TextBox ID="tbxRespName" runat="server" MaxLength="50" TabIndex="29">
</asp:TextBox>
<asp:RequiredFieldValidator ID="vRespNameReq" runat="server" ErrorMessage="<br />Please enter the caregiver's name."
  CssClass="validationError" ControlToValidate="tbxRespName" ForeColor="White"
  BackColor="Red" SetFocusOnError="true" ValidationGroup="valIncident" 
  Display="Dynamic"></asp:RequiredFieldValidator>

<asp:TextBox ID="tbxRespTitle" runat="server" MaxLength="50" TabIndex="29">
</asp:TextBox>
<asp:RequiredFieldValidator ID="vRespTitleReq" runat="server" ErrorMessage="<br />Please enter the caregiver's title."
  CssClass="validationError" ControlToValidate="tbxRespTitle" ForeColor="White"
  BackColor="Red" SetFocusOnError="true" ValidationGroup="valIncident" Display="Dynamic"></asp:RequiredFieldValidator>

Can anyone point me in the right direction? 谁能指出我正确的方向? Thanks! 谢谢!

Edit: Here's the rendered HTML: 编辑:这是呈现的HTML:

<input name="ctl00$MainContent$tbxRespName" type="text" maxlength="50" 
  id="ctl00_MainContent_tbxRespName" tabindex="29" />
<span id="ctl00_MainContent_vRespNameReq" class="validationError" 
  style="color:White;background-color:Red;display:none;"><br />Please enter the 
  caregiver's name.</span>
<input name="ctl00$MainContent$tbxRespTitle" type="text" maxlength="50" 
  id="ctl00_MainContent_tbxRespTitle" tabindex="30" /><br />
<span id="ctl00_MainContent_vRespTitleReq" class="validationError" 
  style="color:White;background-color:Red;display:none;"><br />Please enter the 
  caregiver's title.</span>

As per my comment ... 根据我的评论 ...

The problem is that you're checking for .val() == "Yes" but your RadioButtonList item has Value="Y" . 问题是您正在检查.val() == "Yes"但是RadioButtonList项具有Value="Y"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM