我有一個表單,當被提交時,頁面被重新加載,一個名爲#message的div被創建並顯示在主體中。我試圖ajaxify我的表單,但我堅持如何讓信息現在顯示而無需重新加載。以下是我的代碼如下。在Ajax表單提交後顯示DIV結果
$(document).ready(function(){
$('input#submit').on('click', function() {
$('#form').submit(function() {
$.ajax({ // create an AJAX call...
data: $(this).serialize(), // get the form data
type: $(this).attr('method'), // GET or POST
url: $(this).attr('action'), // the file to call
success: function(response) { // on success..
alert($('div#message').text()); // Alert the message text..fails
}
});
return false; // cancel original event to prevent form submitting
});
});
});
<form action="settings.php" method="post" class="standard-form" id="form">
<input type="text" name="email" id="email" value="">
<input type="password" name="pass" id="pass" size="16" value="" class="password-entry" />
<input type="submit" name="submit" value="<?php _e('Update'); ?>" id="submit" class="small button"/>
</form>
更新:這是我想要的新功能,但是所有被警告是「[對象] [對象]」,並沒有實際的文本。我不確定這意味着什麼。
$(document).ready(function(){
$('input#submit').on('click', function() {
$('#form').submit(function() {
$.ajax({ // create an AJAX call...
data: $(this).serialize(), // get the form data
type: $(this).attr('method'), // GET or POST
url: $(this).attr('action'), // the file to call
success: function(data) { // on success..
alert($('div#message').text(data));
}
});
return false; // cancel original event to prevent form submitting
});
});
});
是什麼在你成功的功能「響應」的內容?另外,嘗試使用e.preventDefault()來防止默認表單提交。在$(「#form」)提交函數中。 – kinduff
這個html是什麼樣的?並幫助調試:現在刪除您的ajax電話。確保你阻止了默認的表單提交。當你不用重定向就可以點擊提交按鈕時,你可以添加你的ajax請求。 – Jasen
@Jasen我不確定你是否在考慮他們是否有重定向問題。沒有,所以我很抱歉,如果不明確。表單不重定向。提交後,我提醒表單提交後,通常會出現的div的內容,而不是ajax – LearntoExcel