我无法弄清楚这里发生了什么,但当我在验证插件中的 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>
最佳答案
form.serialize()
部分应为 $(form).serialize()
。 form
变量是 DOM element, not a jQuery object .
关于jQuery ajax $.post 从验证插件调用时刷新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7237879/