.ajaxform 在验证 submitHandler 中不起作用? [英] .ajaxform not working inside the validation submitHandler?

查看:39
本文介绍了.ajaxform 在验证 submitHandler 中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 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

这篇关于.ajaxform 在验证 submitHandler 中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆