我有一个表单,当被提交时,页面被重新加载,一个名为#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