Webforms - RadiobuttonLists客户端点击事件无法正常工作

时间:2017-12-08 23:46:11

标签: javascript html asp.net webforms

我有一个服务器端RadioButtonList控件,用于呈现客户端代码,如下所示。正如您所看到的,我已附加客户端javascript以在单击任何单选按钮时显示警报。这符合我在网上看到的例子。但是,单击任何单选按钮时,警报不会显示。控制台也没有显示任何错误。可能有什么不对?我只能使用纯javascript而不是jQuery。我在最新的Chrome浏览器中运行它。

<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>

1 个答案:

答案 0 :(得分:0)

不一定是一个答案,但是对于观看,因为对于该公司的渲染而言,这很奇怪:

页面:

<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>

呈现为:

<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>

请注意,我也在web.config中使用它,因此ID在呈现时不会那么疯狂(摆脱很多像ctl00$...这样的东西:

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