e.preventDefault不会阻止表单提交 [英] e.preventDefault doesn't stop form from submitting

查看:181
本文介绍了e.preventDefault不会阻止表单提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码无法正常工作.每当返回的状态为1时,代码不会在第一个if语句中结束,而是继续执行,返回顶部并执行ajax请求:

I have the below code which fails to work as expected. Whenever the status returned is 1, the code does not end in the first if statement but continues to execute returning to the top and executing the ajax request:

$("form#myForm").submit(function(e){
        var formData = new FormData($(this)[0]);
        $.ajax({
            url: "/app/user",
            type: 'POST',
            data: formData,
            async: false,
            success: function (data) {
                var parseData = JSON.parse(data);
                var message = parseData.msg;
                var status = parseData.status;

                if (status == "1"){
                    console.log("one");
                } else if(status == "2"){
                    console.log("two");
                } else if (status == 3){
                    console.log("three");
                }
            },
            cache: false,
            contentType: false,
            processData: false
        });
        e.preventDefault();
    });

推荐答案

您的e.preventDefault()应该在ajax之前调用.

Your e.preventDefault() should be called before the ajax.

这篇关于e.preventDefault不会阻止表单提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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