Yii2 activeform ajax submit and validation

后端 未结 3 1942
北恋
北恋 2021-02-04 09:50

Currently to achieve ajax submit and validation at the same time. I\'m using custom function like:

    $(\'.edit_form\').submit(function (e) {
        e.preventD         


        
3条回答
  •  北恋
    北恋 (楼主)
    2021-02-04 10:29

    Use beforeSubmit event instead of submit, the beforeSubmit will only be triggered once the form passes the validation.

    $('form').on('beforeSubmit', function(e) {
        var form = $(this);
        var formData = form.serialize();
        $.ajax({
            url: form.attr("action"),
            type: form.attr("method"),
            data: formData,
            success: function (data) {
                 ...
            },
            error: function () {
                alert("Something went wrong");
            }
        });
    }).on('submit', function(e){
        e.preventDefault();
    });
    

提交回复
热议问题