You need to use the dynamic version of jQuery's .on()
or .delegate()
.
$('table').on('click', '.click', function() {
// your code here
});
For dynamic behavior using delegated event handling, the basic idea is that the first select (in the jQuery object) must be a static parent object that is not dynamically created after the event handler is installed. The second selector which is passed as the second argument to .on() is a selector that matches the specific item you want the event handler on. These items can be dynamically created after the event handler is installed.
Using .click()
or .bind()
gets you static event handlers that only work on objects that are present at the time the code is initially run.
To make this code more robust, I'd suggest two things. First, don't use a class name like "click" that is very easy to confuse with an event. Second, put an id on your table, so that ID can be referenced in the first selector rather than the very generic "table"
that may accidentally be active on other tables in your page.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…