Submit form via AJAX in jQuery

前端 未结 2 437
误落风尘
误落风尘 2020-12-09 16:47

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

jQuery(\'form.AjaxForm\').submit( function() {            
        $.ajax({
            url     :         


        
2条回答
  •  隐瞒了意图╮
    2020-12-09 17:15

    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).

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

提交回复
热议问题