.ajaxform 在验证 submitHandler 中不起作用?

2022-01-11 00:00:00 jquery jquery-plugins ajax

我使用 jquery 验证插件在提交前验证表单在 submitHandler 我使用 ajax 请求用 ajax 发布表单在我使用 .ajax 发送请求之前,但现在表单有图像并且很难通过正常的ajax请求序列化文件元素,因此我使用了这个插件http://www.malsup.com/jquery/form/

i use the jquery validation plugin to validate form before submit in submitHandler i use ajax request to post the form with ajax before i used .ajax to send the request but now the form have image and its so hard to serialize the file element through the normal ajax request and because of this i used this plugin http://www.malsup.com/jquery/form/

现在使用插件后,ajax 请求无法正常工作,不知道为什么这是第一个使用普通 ajax 调用的示例

now after using the plugin the ajax request not working at it all don't know why this the first example with the normal ajax call

$(document).ready(function(){
    $("#categoryForm").validate({
    submitHandler: function() {
            $.ajax({
                type:'POST', 
                url: 'actions/add-category.php', 
                data: $("#categoryForm").serialize(), 
                success: function(response) {
                     $('#status').html(response);
                    }
                });

        return false;
    }
    });
});

使用插件后的这个

$(document).ready(function(){
        $("#categoryForm").validate({
        submitHandler: function() {
                $('#categoryForm').ajaxForm(function() {
                alert('the form was successfully processed');
            });

            return false;
        }
        });
    });

第二个不工作

推荐答案

尝试反转函数:

jQuery('#form_id').ajaxForm({
    beforeSubmit: function() {
        jQuery("#form_id").validate({
            rules: {
                first_name: "required",
                last_name: "required"
            },
            messages: {
                first_name: "Required",
                last_name: "Required"
            }
        });
        return jQuery('#form_id').valid();
    },
    success: function(resp) {
        jQuery('#resp').html(resp).fadeIn('fast');
    }
});

其中 #resp 是将接收从您的 #form_id 发布的文件生成的 HTML 的 ID

Where #resp is the ID that will receive the HTML generated from the file your #form_id posted

相关文章