jQuery Ajax函数不起作用 [英] jquery ajax function not working
本文介绍了jQuery Ajax函数不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的html就像这样,
my html goes like this ,
<form name="postcontent" id="postcontent">
<input name="postsubmit" type="submit" id="postsubmit" value="POST"/>
<textarea id="postdata" name="postdata" placeholder="What's Up ?"></textarea>
</form>
jquery代码如下
The jquery code is as follows
$("#postcontent").submit(function(e) {
$.ajax({
type:"POST",
url:"add_new_post.php",
data:$("#postcontent").serialize(),
beforeSend:function(){
$(".post_submitting").show().html("<center><img src='images/loading.gif'/></center>");
},success:function(response){
//alert(response);
$("#return_update_msg").html(response);
$(".post_submitting").fadeOut(1000);
}
});
});
当我单击提交按钮时,我的ajax请求不起作用,好像控件正在传递给JQuery Submit函数,但是ajax请求未正确执行/正常工作,这是什么问题呢?
When I click on the submit button , my ajax request is not working , it looks as if the control is being passed to the JQuery submit function , but the ajax request is not executing/working properly,what is wrong ?
推荐答案
put $(document).ready(function(){...})中的事件处理函数。它现在应该可以工作
put the event handler function inside $(document).ready(function(){...}). it shall work now
还添加preventDefault()来限制页面刷新
also add preventDefault() to restrict page refreshing
$(document).ready(function() {
$("#postcontent").submit(function(e) {
e.preventDefault();
$.ajax({
type : "POST",
url : "add_new_post.php",
data : $("#postcontent").serialize(),
beforeSend : function() {
$(".post_submitting").show().html("<center><img src='images/loading.gif'/></center>");
},
success : function(response) {
alert(response);
$("#return_update_msg").html(response);
$(".post_submitting").fadeOut(1000);
}
});
e.preventDefault();
});
});
这篇关于jQuery Ajax函数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文