jQuery: Bind ajaxForm to a form on a page loaded v

2019-04-13 01:56发布

I'm using the ajaxForm plugin for jQuery to submit forms on my webapp. However, in one part of the app, I'm loading some content thathas a form on it via jQuery's .load()

The problem lies in that I can't get ajaxForm to bind to the form loaded via ajax.

I've tried this code to no avail:

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

Any help is REALLY appreciated!!


Edit: Thanks guys! This works perfectly.

4条回答
迷人小祖宗
2楼-- · 2019-04-13 02:22

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

查看更多
SAY GOODBYE
3楼-- · 2019-04-13 02:33
$('#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

});
查看更多
做自己的国王
4楼-- · 2019-04-13 02:34

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    
 });
查看更多
姐就是有狂的资本
5楼-- · 2019-04-13 02:37

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);
});
查看更多
登录 后发表回答