我是 javascript 和 jquery 的新手。我在这段代码中使用了两个 jquery 插件。一个是 Jquery 表单验证器,另一个是 Jquery 表单 ajaxSubmit。对于正常的 ajax 提交,代码工作正常,但现在我也必须发布一个文件。所以我正在使用 ajaxSubmit。当我运行它时,我在浏览器控制台上收到错误“TypeError: e.preventDefault is not a function”。
请不要将此标记为重复问题,因为关于同一主题的其他问题的答案对我不起作用。
<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>
我的问题的解决方案是按如下方式更改提交处理程序:-
submitHandler: function(form){
$(form).ajaxSubmit({
url: '/addpost',
type: 'post',
dataType: 'html',
data : $( "#addpost" ).serialize(),
success : function(data) {
location.reload();
}
});
return false
}
希望这会对某人有所帮助。
最佳答案
我不确定您是否会在提交处理程序中使用 e.preventDefault()
。
删除该行并在您的 ajax 提交之后添加 return false;
。
这将阻止提交表单。
关于javascript - TypeError : e. preventDefault 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36034493/