私は次のスクリプトを使用していますが、問題なく動作するためにはform action attribute
が必要です。行動や提出は、あなたのAJAX形でaction attribute
Ajaxはデフォルトのフォームを現在のページに送信しないようにします
$(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;
}
})
申し訳ありませんが、私はあなたを取得しませんでした。あなたが望むものを説明していただけますか? – mrid
こんにちは@mrid myスクリプトは、 '
サブミットの代わりに入力タイプボタンを使用します。そのボタンにclickイベントを登録し、$(form).submit()関数を削除します。 $(button).click()はアクション属性を削除します。 – ScanQR