jQuery: Bind ajaxForm to a form on a page loaded via .load()

Deadly 提交于 2019-12-04 17:27:18

I think you should put binding code into a callback, because the load is asynchronous:

 $('#viewRecordBtn').live('click', function() { // Handle the event when the 'view record' button is clicked
    $("#tab2").load('ajax/viewRecord.php', function() {
                    $('#formAddRecord').ajaxForm(formAddRecordOptions); // Bind the form
               }); // Load the record and the form into tab 2    
 });

If you use latest jQuery Form Plugin and jQuery 1.7+ you can use 'delegation' option, like this :

$('#myForm').ajaxForm({
    delegation: true,
    target: '#output'
});

Its described in here: http://malsup.github.com/jquery.form.js

that is because you are binding ajaxForm at the time that the .load() is not yet complete. try this:

$('#tab2').load('ajax/viewRecord.php', function() {
  $('#formAddRecord').ajaxForm(formAddRecordOptions);
});
$('#viewRecordBtn').live('click', function() { 
   $("#tab2").load('ajax/viewRecord.php', function(){
       $('#formAddRecord').ajaxForm(formAddRecordOptions); // Bind the form
   }); // Load the record and the form into tab 2

});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!