简体   繁体   中英

Webforms - RadiobuttonLists client side click event not working

I have a server side RadioButtonList control that renders client side code as below. As you can see, I have attached client side javascript to show an alert when any of the radio buttons are clicked. This is in line with the examples I saw online. However, the alert doesn't show when I click any radio button. The console doesnt show any errors either. What could be wrong? I can only use pure javascript and not jQuery. I am running this in the latest Chrome browser.

<span id="ctl00_ContentPlaceHolder1_rblApartmentType">
    <input id="ctl00_ContentPlaceHolder1_rblApartmentType_0" type="radio" name="ctl00$ContentPlaceHolder1$rblApartmentType" value="1" onclick="javascript:alert('1');">
    <label for="ctl00_ContentPlaceHolder1_rblApartmentType_0">Apartment</label>
    <input id="ctl00_ContentPlaceHolder1_rblApartmentType_1" type="radio" name="ctl00$ContentPlaceHolder1$rblApartmentType" value="2" onclick="javascript:alert('2');">
    <label for="ctl00_ContentPlaceHolder1_rblApartmentType_1">Town Home</label>
    <input id="ctl00_ContentPlaceHolder1_rblApartmentType_2" type="radio" name="ctl00$ContentPlaceHolder1$rblApartmentType" value="3" checked="checked" onclick="javascript:alert('3');">
    <label for="ctl00_ContentPlaceHolder1_rblApartmentType_2">Condo</label>
</span>

not necessarily an answer, but for viewing, cuz there's s/t weird with the op's rendering:

page:

<asp:RadioButtonList ID="RadioButtonList1" runat="server">
    <asp:ListItem Text="a" Value="1" onclick="alert('1');"></asp:ListItem>
    <asp:ListItem Text="b" Value="2" onclick="alert('2');"></asp:ListItem>
</asp:RadioButtonList>

renders as:

<table id="RadioButtonList1">
  <tbody>
    <tr>
      <td>
        <input id="RadioButtonList1_0" type="radio"
          name="ctl00$MainContent$RadioButtonList1" value="1" onclick="alert('1');">
        <label for="RadioButtonList1_0">a</label></td>
    </tr>
    <tr>
      <td>
        <input id="RadioButtonList1_1" type="radio"
          name="ctl00$MainContent$RadioButtonList1" value="2" onclick="alert('2');">
        <label for="RadioButtonList1_1">b</label></td>
    </tr>
  </tbody>
</table>

note, i also use this in web.config so the IDs aren't so crazy when rendered (gets rid of a lot of stuff like ctl00$... :

<system.web>
  <pages clientIDMode="Static">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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