0

I am using the following listener:

   $('#datatable .Answer input:radio').on('change', function() {
        alert('Radio Button');

    });

And I add radio button dynamically to this table '#datatable' as follows:

   $(this).html('<input type="radio" class="Answer" name="correct"/>');

where this is the td element in which radio button should be added. Why doesn't the listener work on new elements?

4

1 回答 1

2

为什么侦听器不处理新元素?

因为您应该像这样订阅更改事件:

$('#datatable .Answer').on('change', 'input:radio', function() {
    alert('Radio Button');

});

注意'input:radio'选择器是如何移动到.on()函数中的,以便以活泼的方式订阅事件。

于 2013-07-07T09:53:28.093 回答