开发者

jQuery ajax $.post refreshes page when called from validate plugin

开发者 https://www.devze.com 2023-03-31 10:08 出处:网络
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

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>


The part form.serialize() should be $(form).serialize(). The form variable is a DOM element, not a jQuery object.

0

精彩评论

暂无评论...
验证码 换一张
取 消