通过jQuery AJAX提交表单

时间:2021-12-04 09:47:36

I am using following jQuery code to submit a form via AJAX.

我正在使用以下jQuery代码通过AJAX提交表单。

jQuery('form.AjaxForm').submit( function() {            
        $.ajax({
            url     : $(this).attr('action'),
            type    : $(this).attr('method'),
            dataType: 'json',
            data    : $(this).serialize(),
            success : function( data ) {
                        alert('Submitted')
                      },
            error   : function( xhr, err ) {
                        alert(''Error);     
                      }
        });    
        return false;
    });

Code is working perfectly with no ajax. But does not work if I load form via ajax. I think it is because of loading form via ajax after JavaScript load.

代码完全没有ajax工作。但是如果我通过ajax加载表单,就不能工作。我认为这是因为JavaScript加载后通过ajax加载表单。

Any solution?

有解决方案吗?

2 个解决方案

#1


34  

If using jQuery 1.7+ you could try using .on() to delegate the event and bind to all future forms with the same class. Try finding the closest parent that is not inserted dynamicly instead of $(document).

如果使用jQuery 1.7+,您可以尝试使用.on()来委托事件,并使用相同的类绑定到所有未来的表单。尝试找到最近的没有动态插入的父节点,而不是$(document)。

$(document).on('submit', 'form.AjaxForm', function() {            
        $.ajax({
            url     : $(this).attr('action'),
            type    : $(this).attr('method'),
            dataType: 'json',
            data    : $(this).serialize(),
            success : function( data ) {
                         alert('Submitted');
            },
            error   : function( xhr, err ) {
                         alert('Error');     
            }
        });    
        return false;
    });

#2


1  

You can't attach handlers directly to html that doesn't exist

不能将处理程序直接附加到不存在的html

There are 2 ways to handle it.

有两种处理方法。

Bind the handlers within success callback of ajax.

在ajax的成功回调中绑定处理程序。

    $(formParentSelector).load(formFileUrl, function() {
        /* within this success callback the new html exists and can run code*/
        AjaxForm();
    });

function    AjaxForm(){
    jQuery('form.AjaxForm').submit( function() {            
        $.ajax({
            url     : $(this).attr('action'),
            type    : $(this).attr('method'),
            dataType: 'json',
            data    : $(this).serialize(),
            success : function( data ) {
                        alert('Submitted');
                      },
            error   : function( xhr, err ) {
                        alert('Error');     
                      }
        });    

                                             })
 }

The other way is to delegate the handler to a higher level in the document so it is avalibale for future matching elements

另一种方法是将处理程序委托给文档中的更高级别,以便将来的匹配元素可以使用它

 jQuery(document).on('submit','form.AjaxForm').submit( function() {            
        $.ajax({
            url     : $(this).attr('action'),
            type    : $(this).attr('method'),
            dataType: 'json',
            data    : $(this).serialize(),
            success : function( data ) {
                        alert('Submitted');
                      },
            error   : function( xhr, err ) {
                        alert('Error');     
                      }
        });    

                                             })

#1


34  

If using jQuery 1.7+ you could try using .on() to delegate the event and bind to all future forms with the same class. Try finding the closest parent that is not inserted dynamicly instead of $(document).

如果使用jQuery 1.7+,您可以尝试使用.on()来委托事件,并使用相同的类绑定到所有未来的表单。尝试找到最近的没有动态插入的父节点,而不是$(document)。

$(document).on('submit', 'form.AjaxForm', function() {            
        $.ajax({
            url     : $(this).attr('action'),
            type    : $(this).attr('method'),
            dataType: 'json',
            data    : $(this).serialize(),
            success : function( data ) {
                         alert('Submitted');
            },
            error   : function( xhr, err ) {
                         alert('Error');     
            }
        });    
        return false;
    });

#2


1  

You can't attach handlers directly to html that doesn't exist

不能将处理程序直接附加到不存在的html

There are 2 ways to handle it.

有两种处理方法。

Bind the handlers within success callback of ajax.

在ajax的成功回调中绑定处理程序。

    $(formParentSelector).load(formFileUrl, function() {
        /* within this success callback the new html exists and can run code*/
        AjaxForm();
    });

function    AjaxForm(){
    jQuery('form.AjaxForm').submit( function() {            
        $.ajax({
            url     : $(this).attr('action'),
            type    : $(this).attr('method'),
            dataType: 'json',
            data    : $(this).serialize(),
            success : function( data ) {
                        alert('Submitted');
                      },
            error   : function( xhr, err ) {
                        alert('Error');     
                      }
        });    

                                             })
 }

The other way is to delegate the handler to a higher level in the document so it is avalibale for future matching elements

另一种方法是将处理程序委托给文档中的更高级别,以便将来的匹配元素可以使用它

 jQuery(document).on('submit','form.AjaxForm').submit( function() {            
        $.ajax({
            url     : $(this).attr('action'),
            type    : $(this).attr('method'),
            dataType: 'json',
            data    : $(this).serialize(),
            success : function( data ) {
                        alert('Submitted');
                      },
            error   : function( xhr, err ) {
                        alert('Error');     
                      }
        });    

                                             })