【问题标题】:Webforms - RadiobuttonLists client side click event not workingWebforms - RadiobuttonLists 客户端点击事件不起作用
【发布时间】:2017-12-08 23:46:11
【问题描述】:

我有一个服务器端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>

【问题讨论】:

  • 这个例子对我有用.. :thinking-face: codepen.io/anon/pen/POMbqo
  • 这家伙说使用 onClick >> juristr.com/blog/2008/06/attaching-client-side-event-handler-to 但我无法让它与 onClick 一起使用。它使用 onChange 工作! ://
  • 由于我们无法访问您的服务器端的东西,我建议您花一些时间学习开发工具:) 在服务器端生成代码后尝试检查当前元素。查看是否存在 onClick 元素。
  • 发布整个渲染页面怎么样?
  • 好吧,如果它被渲染到 dom 上......它应该可以工作:/ 我认为如果没有更多代码,我们无法进一步帮助您

标签: javascript html asp.net webforms


【解决方案1】:

不一定是答案,但为了查看,因为操作的渲染有点奇怪:

页面:

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

【讨论】:

    猜你喜欢
    • 2020-05-20
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多