2017-07-09 14 views
0

我有一個表單,我正在驗證jQuery。如何讓jQuery將表單提交到帖子

<form id="target" class="center-block" action="/" method="POST"> 
    <textarea class="form-control" id="name" rows="3" name="name" placeholder="Enter a burger you would like to devour!"></textarea> 
    <button type="button" id="submit" class="center-block btn btn-default top">Submit</button> 
</form> 

驗證後我也試圖通過jQuery提交表單。

if (validateForm() == true) { 
    $("#target").submit(); 
} 

驗證工作正常,但似乎沒有表單提交到郵件路徑。我能夠讓表單使用ajax發佈,但帖子在完成後不會重定向。希望這種方法能夠產生預期的效果。

該應用程序在Express上運行並使用MySQL。

+0

通過Ajax或不? –

+0

您正在尋找什麼「期望的效果」?如果您要提交到同一頁面,請執行/退出操作。 – Difster

+0

如果驗證工作,並且頁面重定向,它就可以工作。沒有人能做更多。 – adeneo

回答

0

我不認爲你的jquery submit將工作,因爲一個元素id or name submit將取代form.submit方法,你應該簡單地改變id(名字也不應該給)。

<button type="button" id="changeThisId" class="center-block btn btn-default top">Submit</button> 
+0

現在我感到很蠢,因爲它也在附加說明的文檔中說過。 [https://api.jquery.com/submit/](https://api.jquery.com/submit/) – Adam

0

使用Ajax,如果你想重定向到當前頁面,成功刷新當前頁面

$.ajax({  
    url: yourUrl, 
    type: "post", 
    data: $("#target").serialize(), 
    success: function(r){ 
     //form posted successfully 
     window.location.reload(); 
    }, 
    error: function(error){ 
     alert(error); 
    } 
}); 
0

我會在那裏有一個錯字,但這個想法是:

$('#target').submit(function(event){ 
    event.preventDefault(); 
}).validate({ 
    rules: { ... }, 
    submitHandler: function(form){ 
     $.ajax{ 
      type: 'POST', 
      data: $(form).serialize(), 
      url: '/', 
      success: function(result){ 
       console.log(result); 
      } 
      error: function(err){ 
       console.log(err.message); 
      } 
     } 
    }); 
});