Jquery not working for class that is loaded through AJAX
I have loaded a select box using jquery ajax and the select box has a class for which i 开发者_开发知识库have written a .change event which is not working.
How do i solve the problem.
Shud i add some coding to set jquery to rescan the page for new classes?
Please comment
If you are loading the control using ajax the controls will not be there on load of the document so the .bind and .change will not work.
.change() is equivalent to .bind()
instead use the .delegate()
$("body").delegate(".selector", 'change', handler);
Which will attach the handler when the control is available in the document.
live can also be used, however live has performance issues particularly with IE. I will suggest go for .delegate()
You should take a look at the live function...
Something like:
$('.selector').live('change', function(){
// Your code...
});
This should bind the function to your element whenever it is put on the page - not just the first time the page loads.
精彩评论