Javascript 类型错误:e.preventDefault 不是函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36034493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:32:26  来源:igfitidea点击:

TypeError: e.preventDefault is not a function

javascriptjqueryajax

提问by Arjun Ajith

I am new in javascript and jquery. I am using two jquery plugins in this code. One is Jquery form validator and the other is Jquery form ajaxSubmit. With normal ajax submission the code works fine, but now i had to post a file too. So I am using ajaxSubmit. When I run this I get an error "TypeError: e.preventDefault is not a function" on browser console.

我是 javascript 和 jquery 的新手。我在这段代码中使用了两个 jquery 插件。一个是 Jquery 表单验证器,另一个是 Jquery 表单 ajaxSubmit。通过正常的ajax提交,代码工作正常,但现在我也不得不发布一个文件。所以我使用ajaxSubmit。当我运行它时,我在浏览器控制台上收到错误“TypeError: e.preventDefault is not a function”。

Please don't mark this as a duplicate question because answers for other questions on the same topic are not working for me.

请不要将此标记为重复问题,因为同一主题的其他问题的答案对我不起作用。

<script type="text/javascript">
    $(document).ready(function() {

        $("#addpost").validate({
            rules: {
                subject: "required",
                comment: "required"
            },
            messages: {
                subject: "Please enter a subject",
                comment: "Please enter some details",                   
            },
            submitHandler: function(e){
                e.preventDefault();
                $.ajaxSubmit({
                    url: '/addpost',
                    type: 'post',
                    dataType: 'html',
                    data : $( "#addpost" ).serialize(),
                    success : function(data) {
                        location.reload();
                    }
                });
            }
        });     

    }); 
</script>

Solution for my problem was changing the submithandler as follows:-

我的问题的解决方案是按如下方式更改提交处理程序:-

submitHandler: function(form){
    $(form).ajaxSubmit({
            url: '/addpost',
            type: 'post',
            dataType: 'html',
            data : $( "#addpost" ).serialize(),
            success : function(data) {
                location.reload();
            }
     });
    return false
}

Hoping this will help someone.

希望这会帮助某人。

采纳答案by mtrolle

I'm not sure you would use e.preventDefault()in a submit handler.

我不确定你会e.preventDefault()在提交处理程序中使用。

Remove that line and add return false;after your ajax submit instead. That would prevent the form from being submitted.

删除该行并return false;在您的 ajax 提交后添加。这将阻止提交表单。

回答by Imesh Chandrasiri

The eyou are using inside the submitHandlerdoes not provide an event object. It provides the form object which you are validating! Thus the error you are getting. Please read the documentation 1so that you get an idea on what objects and callback you are dealing with.

e您使用内submitHandler不提供的事件对象。它提供了您正在验证的表单对象!因此你得到的错误。请阅读文档1,以便您了解正在处理的对象和回调。

[1 - link]

[1 - 链接]

回答by wahmal

your enot event, but the form element. change your code like this :

e不是事件,而是表单元素。像这样改变你的代码:

submitHandler: function(form,e){ // place your element on second parameter
            e.preventDefault();
            $.ajaxSubmit({
                url: '/addpost',
                type: 'post',
                dataType: 'html',
                data : $( "#addpost" ).serialize(),
                success : function(data) {
                    location.reload();
                }
            });
        }