2016-11-13 134 views
1

我有以下脚本,这完美的作品,但问题是我需要一个form action attribute它的工作,因此我的问题如何修改我的当前脚本,它可以防止默认表单提交行为和形式的提交当前页面上,而不需要一个action attribute形式阿贾克斯防止默认和提交表格当前页

$(function() { 
    var form = $('#editRes'); 
    var formMessages = $('#formMsg'); 
    // Set up an event listener for the contact form. 
    $(form).submit(function(e) { 
    // Stop the browser from submitting the form. 
    e.preventDefault(); 
    //do the validation here 
    if (!validateLog()) { 
     return; 
    } 
    // Serialize the form data. 
    var formData = $(form).serialize(); 
    // Submit the form using AJAX. 
    $.ajax({ 
     type: 'POST', 
     url: $(form).attr('action'), 
     data: formData 
    }).done(function(response) { 
     // Make sure that the formMessages div has the 'success' class. 
     $(formMessages).removeClass('error').addClass('success'); 
     // Set the message text. 
     $(formMessages).html(response); // < html(); 
     // Clear the form. 
     $('').val('') 
    }).fail(function(data) { 
     // Make sure that the formMessages div has the 'error' class. 
     $(formMessages).removeClass('success').addClass('error'); 
     // Set the message text. 
     var messageHtml = data.responseText !== '' ? data.responseText : 'Oops! An error occured!.'; 
     $(formMessages).html(messageHtml); // < html() 
    }); 

    }); 
    function validateLog() { 
    var valid = true; 
    //VALIDATE HERE 
    return valid; 
    } 
}) 
+0

对不起,但我没有得到你。你能解释一下你想要的吗? – mrid

+0

您好@mrid我的脚本只能用于

'但是当我有一个像的形式时,即在页面中提交没有'action'属性的脚本在表单字段我的脚本不起作用....有道理? –

+0

使用输入类型按钮而不是提交。在该按钮上注册单击事件并删除$(form).submit()函数。 $(button).click()将删除您的操作属性。 – ScanQR

回答

0

在你的Ajax,您使用的url: $(form).attr('action')。这意味着表单将被提交给您的表单的action属性。由于没有,阿贾克斯将无法正常工作。

如果你想要的形式是没有action标签,你可以只写在一个AJAX网址部分

$.ajax({ 
    type: 'POST', 
    url: 'page.php', 
    data: formData, 
    ... 
    ... 
}); 
+0

不需要动作属性。删除提交并使用type =“button”并在此按钮上注册点击事件。 Ajax url将是action属性的值。 – ScanQR

+0

对不起,我没有让你'你可以只写表格提交网址(page.php)'你可以请扩大.... @TechBreak我不能删除我的提交按钮 –

+0

@TimothyCoetzee你为什么不能?任何具体原因?根据您的问题,您想在当前页面上提交表格。 – ScanQR

0

另一种选择是window.location.href表单提交的url(page.php文件)。

附注:您不需要重新包装form$(),因为它已经是您第二行中的jQuery对象 - 与formMessages一样。

$(function() { 
    var form = $('#editRes'); // this is a jQuery object 
    var formMessages = $('#formMsg'); // this is also a jQuery object 

    // Set up an event listener for the contact form. 
    form.submit(function (e) { 
     // Stop the browser from submitting the form. 
     e.preventDefault(); 
     // do the validation here 
     if (!validateLog()) { 
      return; 
     } 
     // Submit the form using AJAX. 
     $.ajax({ 
      type: 'POST', 
      url: window.location.href, 
      data: form.serialize() 
     }).done(function(response) { 
      // Make sure that the formMessages div has the 'success' class. 
      formMessages.removeClass('error').addClass('success'); 
      // Set the message text. 
      formMessages.html(response); // < html(); 
      // Clear the form. 
      $('').val('') 
     }).fail(function(data) { 
      // Make sure that the formMessages div has the 'error' class. 
      formMessages.removeClass('success').addClass('error'); 
      // Set the message text. 
      var messageHtml = data.responseText !== '' ? data.responseText : 'Oops! An error occured!.'; 
      formMessages.html(messageHtml); // < html() 
     }); 
    }); 

    function validateLog() { 
     var valid = true; 
     //VALIDATE HERE 
     return valid; 
    } 
}); 
0

在您提交功能添加结束:

return false; 

这样可以防止浏览器导航了。