Javascript onsubmit="return false" 对 Internet Explorer 7/8 没有影响(表单仍然提交)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4078245/
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
onsubmit="return false" has no effect on Internet Explorer 7/8 (form is still submitted)
提问by Dirk Paessler
I have a form that will be submitted by javascript code triggered in "onsubmit" of the tag. Works fine on all browsers - but not on IE7/IE8.
我有一个表单,它将由在标签的“onsubmit”中触发的 javascript 代码提交。适用于所有浏览器 - 但不适用于 IE7/IE8。
What can I do?
我能做什么?
<form action="/dosomething.htm" method="GET" onsubmit="submitmyform();return false">
[...]
<input type="submit" value="Go">
</form>
回答by Drew
I'm going to nitpick this. If you want to handle form submissions, that is what submit is for. If the user hits enter in one of your fields, your onclick handler will be totally avoided. Here is a basic example of doing this in a non-obtrusive way.
我要挑剔这个。如果您想处理表单提交,这就是 submit 的用途。如果用户在您的字段之一中点击输入,您的 onclick 处理程序将被完全避免。这是以非侵入式方式执行此操作的基本示例。
<form name="myform">
<input type="submit" />
</form>
<script>
document.myform.onsubmit = function(){
alert('handled');
return false;
}
</script>
This can be made a lot simpler with jQuery, same form...
这可以用 jQuery 简化很多,同样的形式......
$("form[name=myform]").bind('submit',function(){
alert('handled');
return false;
});
回答by Thibault Witzig
Several ideas proposed here work (because they use different ways to write correct code), but there is a much easier answer
这里提出的几个想法有效(因为它们使用不同的方式来编写正确的代码),但有一个更简单的答案
OP wrote :
OP写道:
onsubmit="submitmyform();"
instead of :
代替 :
onsubmit="return submitmyform();"
That's it.
就是这样。
回答by Ally
I don't think your return false is ever reached, as it comes after what's returned from your function.
我认为您的 return false 永远不会达到,因为它是在您的函数返回的内容之后。
<form action="/dosomething.htm" method="GET" onsubmit="submitmyform();return false">
Make sure that you return false inside of your 'submitmyform()' function else, if it's not then it could be returning true to you form obsubmit event.
确保你在你的“submitmyform()”函数内返回false,否则它可能会返回true给你表单obsubmit事件。
回答by Sergiu
<script type="text/javascript">
<!--
function submitHandler()
{
alert(0);
return false;
}
window.onload=function(){document.getElementById("formid").attachEvent("onsubmit", submitHandler);}
-->
</script>
<form action="/dosomething.htm" method="GET" id="formid">
[...]
<input type="submit" value="Go">
</form>
The attach event method only works only for IE7/8. If you want a reliable cross browser solution you should use addEventListener as an alternative.
attach 事件方法仅适用于 IE7/8。如果您想要一个可靠的跨浏览器解决方案,您应该使用 addEventListener 作为替代方案。
Hope it helps
希望能帮助到你
回答by Husnu Setiawan
try this. work for me.
尝试这个。为我工作。
onSubmit="javascript:return false"
onSubmit="javascript:return false"
回答by Dissident Rage
To cancel an event in legacy IE, you need to set returnValue
to false
. Simply returning false
won't do it. I don't know why Microsoft implemented it in this way.
要取消旧版 IE 中的事件,您需要设置returnValue
为false
. 简单地返回是false
不行的。我不知道微软为什么以这种方式实现它。
function validateForm(evt) {
// if form is not valid
if(!condition) {
// if evt has preventDefault
if(evt.preventDefault)
{ event.preventDefault(); }
// if evt has returnValue
else if(evt.returnValue)
{ evt.returnValue = false; }
// fallback
else
{ return false; }
}
}
// assume form is reference to your form
form.attachEvent('onsubmit',validateForm);
回答by redestructa
I had the same error and when I turned on the console, it never came. So I figured out, it was the console, that wasnt known by the internet explorer as long as it is turned off. Simply add a dummy console to the window with the following code (if there isnt already one):
我遇到了同样的错误,当我打开控制台时,它从未出现。所以我想通了,它是控制台,只要它关闭,Internet Explorer 就不会知道它。只需使用以下代码向窗口添加一个虚拟控制台(如果还没有):
var myconsole = {log : function(param){}}
window.console = window.console || myconsole;
With this you could get in trouble, if you want to have microsofts javascript console. Then just comment the lines out. But now, the console object is not undefined anymore on the whole site.
如果您想拥有 microsofts javascript 控制台,那么您可能会遇到麻烦。然后只需注释掉这些行。但是现在,控制台对象在整个站点上不再是未定义的。
回答by Atiqur
<form action="/dosomething.htm" method="GET" onsubmit="return submitmyform(this)">
[...]
<input type="submit" value="Go">
</form>
this works fine... the function must return true or false
这工作正常......该函数必须返回true或false
回答by Dirk Paessler
The solution for us was to move the javascript event from "onsubmit" of the form to "onclick" of the submit button.
我们的解决方案是将 javascript 事件从表单的“onsubmit”移动到提交按钮的“onclick”。
<form action="/dosomething.htm" method="GET">
[...]
<input type="submit" value="Go" onclick="submitmyform();return false">
</form>
回答by Tibi
In fact, because you write submitmyform();return false
only submitmyform
is evaluated.
事实上,因为你submitmyform();return false
只写了submitmyform
评估。
In the case of two commands, you will have to put them between braces like this
在两个命令的情况下,您必须像这样将它们放在大括号之间
{submitmyform();return false;}
so that both are evaluated.
以便对两者进行评估。