TypeError:e。preventDefault不是函数

时间:2022-03-03 15:44:18

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不是浏览器控制台上的一个函数。

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.

希望这能帮助某人。

3 个解决方案

#1


3  

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提交之后。这将阻止表单提交。

#2


1  

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

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

[1 - link]

(1 -链接)

#3


0  

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

不是事件,而是表单元素。像这样更改代码:

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();
                }
            });
        }

#1


3  

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提交之后。这将阻止表单提交。

#2


1  

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

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

[1 - link]

(1 -链接)

#3


0  

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

不是事件,而是表单元素。像这样更改代码:

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();
                }
            });
        }