2010-02-05 14 views
2

J'utilise le plugin ajaxForm pour jQuery pour soumettre des formulaires sur ma webapp. Cependant, dans une partie de l'application, je charge un contenu qui a une forme sur elle via .load()jQuery: Lier ajaxForm à un formulaire sur une page chargée via .load()

Le problème réside dans le fait que je ne peux pas obtenir ajaxForm pour se lier à la forme chargée via ajax.

J'ai essayé ce code en vain:

$('#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 
}); 

Toute aide est vraiment apprécié !!


Édition: Merci les gars! Cela fonctionne parfaitement.

Répondre

7

Je pense que vous devez mettre le code de liaison dans un rappel, car la charge est asynchrone:

$('#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  
}); 
0
$('#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 

}); 
1

qui est parce que vous liez ajaxForm au moment où le .load() est pas encore terminée. essayez ceci:

$('#tab2').load('ajax/viewRecord.php', function() { 
    $('#formAddRecord').ajaxForm(formAddRecordOptions); 
}); 
5

Si vous utilisez le dernier formulaire jQuery Plugin et jQuery 1.7+ vous pouvez utiliser l'option 'délégation', comme ceci:

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

Son décrit ici: http://malsup.github.com/jquery.form.js

+1

Cela devrait soyez la réponse acceptée! Merci! –

+0

Je veux te faire un câlin. –

+0

Merci pour la solution simple! –