本文介绍了AJAX表单验证问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图用ajax做一个评论系统,它工作得很好,但是我想用它做验证,因为如果hte形式的值是空的并且我不知道是什么问题,我不希望它提交是.
以下是ajax代码:

So i am trying to do a comment system with ajax and it works perfectly but i am trying to do validation with it because i do not want it to submit if the value of hte form is empty and i cannot figure what the problem is.
The follow ins the ajax code :

 $(document).ready(function(){

   $(document).on(''click'',''.submitComment'',function(e) {

        e.preventDefault();
        //send ajax request
        var form = $(this).closest(''form'');
        var formValue = $(this).closest(''form'').val();
        if (formValue.length > 0)
        {
        $.ajax({
            url: ''ajax_comment.php'',
            type: ''POST'',
            cache: false,
            dataType: ''json'',
            data: $(form).serialize(), //form serialize data
            beforeSend: function(){
                //Changeing submit button value text and disableing it
                $(this).val(''Submiting ....'').attr(''disabled'', ''disabled'');
            },
            success: function(data)
            {
                var item = $(data.html).hide().fadeIn(800);
                $(''.comment-block_'' + data.id).append(item);

                // reset form and button
                $(form).trigger(''reset'');
                $(this).val(''Submit'').removeAttr(''disabled'');
            },
            error: function(e)
            {
                alert(e);
            }
        });
    }
    else
    {
        alert("Please fill in the form");

    }
    });
});


即使填写了表单,它也只会继续处理条件的其他部分.


It just keep going to the else part of the condistion even when the form is filled up.

推荐答案




这篇关于AJAX表单验证问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 20:58