简体   繁体   English

带有ajax提交处理程序的jquery验证插件不起作用

[英]jquery validation plugin with ajax submit handler not working

I've used the jquery validation plugin quite a bit in the last few days, but have yet to use it with an ajax submit. 在过去的几天中,我已经大量使用了jquery验证插件,但尚未将其与ajax提交一起使用。 What I have is below cut down to two fields. 下面是我要缩小的两个字段。 There are no errors for the values when submitting. 提交时,这些值没有错误。 There is no submission happening whatsoever when clicking the submit button. 单击提交按钮时,没有任何提交。 It just does nothing. 它什么也没做。

HTML: HTML:

<form id="account-info-form" action="/process/p_profile_info.php" method="post">
    <div class="row margin-bottom-20">
        <div class="col-md-6 form-group">
            <label>First Name</label>
            <div class="input-group">
                <span class="input-group-addon">
                    <i class="fa fa-user fa-fw"></i>
                </span>
                <input class="form-control" type="text" name="fname"/>
            </div>
        </div>
        <div class="col-md-6 form-group">
            <label>Last Name</label>
            <div class="input-group">
                <span class="input-group-addon">
                    <i class="fa fa-user fa-fw"></i>
                </span>
                <input class="form-control" type="text" name="lname"/>
            </div>
        </div>
    </div>
    <div class="row margin-bottom-30">
        <div class="col-md-12">
            <button class="btn btn-primary" type="submit" name="account-info" value="save"><i class="fa fa-check-circle"></i> Save Changes</button>
            <button class="btn btn-default" type="reset">Cancel</button>
        </div>
    </div>
</form>

JS: JS:

$('#account-info-form').validate({          
    // ajax submit
    submitHandler: function (form) {
    var $form = $(this);
        $.ajax({
            type: $form.attr('method'),
            url: $form.attr('action'),
            data: $form.serialize(),
            dataType : 'json'
        })
        .done(function (response) {
            if (response.success == 'success')
            {               
        alert('success');                       
            } 
            else
            {
                alert('fail');
            }
        });
    return false; // required to block normal submit since you used ajax
    }
});

There is no reason to do this, (and $(this) is not what you're expecting it to be)... 没有理由这样做(( $(this)不是您期望的那样)...

var $form = $(this);

Simply use the form argument that's passed into the function. 只需使用传递给函数的form参数。

submitHandler: function (form) {
    $.ajax({
        type: $(form).attr('method'),
        url: $(form).attr('action'),
        data: $(form).serialize(),
        dataType : 'json'
    })
    .done(function (response) {
        if (response.success == 'success') {               
            alert('success');                       
        } else {
            alert('fail');
        }
    });
    return false; // required to block normal submit since you used ajax
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM