jQuery ajax $.post 从验证插件调用时刷新页面
我无法弄清楚这里发生了什么,但当我在验证插件中的 submitHandler
内部调用 $.post
时,我的页面正在刷新/重定向。后端只是简单地返回 HTTP 状态代码 200
。
这是我的代码:
$('form[name=comment]').validate({
submitHandler: function(form) {
$('#postComment').attr('disabled', 'disabled');
$.post('/blog/comments/create/', form.serialize(), function() {
prependComment(form);
return false;
}).error(function() {
alert('There was an issue when submitting this comment.');
}).complete(function() {
$('#postComment').removeAttribute('disabled');
});
return false;
}
});
这是 HTML
<form action="/blog/comments/create/" name="comment" method="post">
<input type="hidden" name="post" value="1"/>
<ul>
<li>
<input type="text" name="name" value="Your name" class="required"/>
</li>
<li>
<textarea name="message" rows="10" cols="55" class="required"></textarea>
</li>
<li>
<button type="submit" id='postComment' class="primary">Post comment</button>
</li>
</ul>
</form>
I can not figure out what is happening here but somehow my page is being refreshed/redirected when I call $.post
inside of the submitHandler
in the validate plugin. The backend is just simply returning an HTTP status code of 200
.
Here is my code:
$('form[name=comment]').validate({
submitHandler: function(form) {
$('#postComment').attr('disabled', 'disabled');
$.post('/blog/comments/create/', form.serialize(), function() {
prependComment(form);
return false;
}).error(function() {
alert('There was an issue when submitting this comment.');
}).complete(function() {
$('#postComment').removeAttribute('disabled');
});
return false;
}
});
Here is the HTML
<form action="/blog/comments/create/" name="comment" method="post">
<input type="hidden" name="post" value="1"/>
<ul>
<li>
<input type="text" name="name" value="Your name" class="required"/>
</li>
<li>
<textarea name="message" rows="10" cols="55" class="required"></textarea>
</li>
<li>
<button type="submit" id='postComment' class="primary">Post comment</button>
</li>
</ul>
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
form.serialize()
部分应为$(form).serialize()
。form
变量是一个 DOM 元素,而不是 jQuery 对象。The part
form.serialize()
should be$(form).serialize()
. Theform
variable is a DOM element, not a jQuery object.