如何检测新单选按钮上的更改?

问题描述 投票:0回答:1

我有检测单选按钮更改,然后在按钮上调用方法的代码。它可以在现有按钮上正常工作,但是当我向页面中添加新的单选按钮时(通过ajax),它对它们不起作用。如何获得检测新单选按钮更改的信息?

这是我的咖啡脚本代码:

jQuery ->
    $(".radio-button:radio").change ->
        doSomething(this)

在JS中看起来像这样:

jQuery(function() {
  return $(".radio-button:radio").change(function() {
    return doSomething(this);
  });
});
jquery ruby-on-rails coffeescript radio-button jquery-events
1个回答
1
投票

对于动态创建的元素,您需要使用.on()进行事件委托>

$(document).on('change','.radio-button:radio', function() {
    //Your code here
});

而不是(对于静态元素)

© www.soinside.com 2019 - 2024. All rights reserved.