[英]Submit JQuery Form $.post() handle display message on success
我有一個表單要用$ .post()在jquery中提交。
$.post("testpage.php", $("#payment-form").serialize())
該帖子本身可以正常工作,但是完成該帖子並獲得成功后,該頁面將執行其他操作,例如顯示感謝消息和其他內容。 我只需要在哪里打電話就可以顯示消息。我不明白從郵件中返回成功消息的意義。
您可以為.post()指定第三個參數,即“成功”回調。 .post執行成功時,將調用此函數。
$.post("testpage.php", $("#payment-form").serialize(), function() { alert('post was successful!')})
簡而言之,像這樣:
$.post("testpage.php", $("#payment-form").serialize(), function () {
// Start partying here.
}).fail(function() {
// Handle the bad news here.
})
另外,您可以使用延遲對象 ,如下所示:
// Create POST request
var paymentPost = $.post("testpage.php", $("#payment-form").serialize());
// Assigned deferred objects
// "data" refers to the data, preferably in JSON format, returned by testpage.php
paymentPost
.done(function(data) {
// Success
// e.g. display thank you message, redirect to a payment successful page...
})
.fail(function(data) {
// If error
// e.g. display error message(s)
})
.always(function() {
// Will always fire as long as POST request is submitted and completed
});
p / s:請注意,不建議使用諸如.success()
和.error()
類的.success()
方法,這一點很重要。 為最終將其移除做准備,您應該遵守有關延遲對象的新術語;)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.