jQuery $(this).closest("form").submit(); not execu


jQuery $(this).closest("form").submit(); not executing

有人能告诉我为什么我的提交没有被执行吗?日志告诉我:"验证通过,将提交表单",但不是提交?

$(function(){
    $("#ajax-payment-form input[type='submit']").click(function(e) {
        // Prevent form submission
        e.preventDefault();
        // Serialize data, make AJAX call
        var str = $("#ajax-payment-form").serialize();
        $.ajax({
            type: "POST",
            url: templateDir+"/payment_form/payment_process.php",
            data: str,
            context: this
        }).done(function(msg) {
            // If a response is received from your server
            if(msg == 'OK') {
                console.log('Validation passed, will submit form');
                $(this).closest("form").submit();
            } else {
                 console.log(msg);
            }
        }).fail(function() {
            console.log('AJAX error');
        });
    });                                                         
});

感谢您抽出时间

thar

只需执行以下操作:

$(function(){
$("#ajax-payment-form input[type='submit']").click(function(e) {
    // Prevent form submission
    var $this = $(this);
    e.preventDefault();
    // Serialize data, make AJAX call
    var str = $("#ajax-payment-form").serialize();
    $.ajax({
        type: "POST",
        url: templateDir+"/payment_form/payment_process.php",
        data: str,
        context: this
    }).done(function(msg) {
        // If a response is received from your server
        if(msg == 'OK') {
            console.log('Validation passed, will submit form');
            $this.closest("form").submit();
   //....

或更短的代码:(通知取消提交事件)

$(function(){
  $("#ajax-payment-form").submit(function(e) {
    e.preventDefault();
    var $form = $(this);
    $.post(
        url: templateDir+"/payment_form/payment_process.php",
        $form.serialize(),
        function(){
            if(msg == 'OK') {
                console.log('Validation passed, will submit form');
                $form.closest("form").submit();
            } else {
                console.log(msg);
            }
        },
        'json'
    ).fail(function() {
        alert( "error" );
     });
  }); 
});