jQuery:将ajaxForm绑定到通过.load()加载的页面上的表单

Bre*_*dan 2 forms ajax jquery bind ajaxform

我正在使用jQuery的ajaxForm插件在我的webapp上提交表单.但是,在应用程序的一部分中,我正在通过jQuery的.load()加载一些内容,其中包含一个表单.

问题在于我无法将ajaxForm绑定到通过ajax加载的表单.

我试过这段代码无济于事:

 $('#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
 });
Run Code Online (Sandbox Code Playgroud)

任何帮助真的很感激!!


编辑:谢谢你们!这非常有效.

Viv*_*ath 7

我认为你应该将绑定代码放入回调中,因为加载是异步的:

 $('#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    
 });
Run Code Online (Sandbox Code Playgroud)


小智 5

如果您使用最新的jQuery Form Plugin和jQuery 1.7+,您可以使用'delegation'选项,如下所示:

$('#myForm').ajaxForm({
    delegation: true,
    target: '#output'
});
Run Code Online (Sandbox Code Playgroud)

它在这里描述:http://malsup.github.com/jquery.form.js