从validate插件调用时,jQuery ajax $ .post刷新页面

时间:2011-08-30 01:41:27

标签: jquery ajax event-handling

我无法弄清楚这里发生了什么,但是当我在validate插件中的$.post内调用submitHandler时,我的页面正在被刷新/重定向。后端只是返回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>

1 个答案:

答案 0 :(得分:1)

部分form.serialize()应为$(form).serialize()form变量为DOM element, not a jQuery object