我正在提交表单,我想阻止提交操作,直到我执行一些其他操作,例如显示文件上传的进度条。我根据诸如event.preventDefault() function not working in IE等问题设置了此代码。
$(document).ready(function(){
$("#fileform").submit(function(e){
if(!e) e = window.event;
if (e.preventDefault){
// Firefox, Chrome
e.preventDefault();
}else{
// Internet Explorer
e.returnValue = false;
}
// other code
}
}
Firefox和Chrome中一切正常;阻止提交操作,出现进度条,然后在我告诉时提交表单。但是,在IE 10中,执行if语句的第一个条件并且表单似乎正在尝试提交(IE 10提交的气泡显示)。为什么第一个条件被执行?
IE 10是否支持在IE 8和9中没有的preventDefault()?如果是这样,我如何正确处理阻止IE 8,9,10中不会干扰Firefox,Chrome,Safari等的表单提交操作......?
答案 0 :(得分:8)
只需执行以下操作,jQuery已经为您完成了跨浏览器工作。
$("#fileform").submit(function(e){
e.preventDefault();
// other code
});
答案 1 :(得分:0)
还要尝试以下内容:
e.stopPropagation();
之后
e.preventDefault();
http://api.jquery.com/event.preventDefault/
http://api.jquery.com/event.stopPropagation/
如果您阅读:Stop form from submitting , Using Jquery
你会看到:Becoz e.preventDefault() is not supported in IE. In IE it is e.returnValue = false
修改后的代码:
$("#fileform").submit(function(e){
if(!e) e = window.event;
e.preventDefault();
e.stopPropagation();
// other code, do something here
return true; // or false if something goes wrong
}