Javascript 返回 false 在 ie 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5229313/
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-10-25 16:27:29  来源:igfitidea点击:

Javascript return false not working in ie

javascriptjspstruts2

提问by ashishjmeshram

HI All

大家好

I have following lines in my JSP.

我的 JSP 中有以下几行。

<s:submit name="submit"  onclick="return validateUser();" action="saveUser"  theme="simple" value="Save" />

The java script method validateUser(), validates the user and returns true or false. The form should not be submitted when the validation fails.

java 脚本方法validateUser() 验证用户并返回true 或false。验证失败时不应提交表单。

This is working in the FF but not in the IE8.

这在 FF 中有效,但在 IE8 中无效。

IE8 submits the form even after validation fails.

即使验证失败,IE8 也会提交表单。

回答by mplungjan

Never assume you can cancel the submit button, instead set some javascript variable or hidden field on the form and use onsubmit. Take my word for it. Have the onsubmit look at the variable set by the different submit buttons

永远不要假设您可以取消提交按钮,而是在表单上设置一些 javascript 变量或隐藏字段并使用 onsubmit。相信我的话。让 onsubmit 查看由不同提交按钮设置的变量

Never use javascript:(javascript colon) unless you are in IE and have a VBScript as the first script on the page. In all other cases javascript is default.

永远不要使用javascript:(javascript 冒号),除非您在 IE 中并且将 VBScript 作为页面上的第一个脚本。在所有其他情况下,javascript 是默认值。

Never use such atrocities as <a href="javascript:something()"instead of <a href="#" onclick="return something()

永远不要使用这种暴行<a href="javascript:something()"代替<a href="#" onclick="return something()

Lastly, in IE, when you have an error occurring, the default action is to submit the form. You may very well have other errors completely elsewhere and have the validate return the error, which is seen as true (0 evaluates to false, most anything else is true)

最后,在 IE 中,当您发生错误时,默认操作是提交表单。您很可能在其他地方完全有其他错误,并且验证返回错误,这被视为真(0 评估为假,其他大多数都是真

<script type="text/javascript">
var isvalidateNeeded = true;
function validate(theForm) {
  if (!isvalidateNeeded) return true; // allow submission
. // normal validation
.
.
  return true; // allow submission
}
</script>
<form onsubmit="return validate(this)">
.
.
.
<input type="submit" name="subaction" value="Test" onclick="isvalidateNeeded=false" />
<input type="submit" name="subaction" value="Check" onclick="isvalidateNeeded=false" />
<input type="submit" name="subaction" value="Submit" onclick="isvalidateNeeded=true" />
</form>

回答by BGerrissen

First, a single form that can perform multiple actions is a bad idea, that said...

首先,可以执行多个操作的单个表单是一个坏主意,也就是说......

Do not use submit buttons, instead:

不要使用提交按钮,而是:

<button type="button" onclick="javascript:validateUser();">Save user</button>
<button type="button" onclick="javascript:deleteUser();">Delete user</button>

Now you only have to worry about default submit behaviour of a form (when user presses enter in a field).

现在您只需要担心表单的默认提交行为(当用户在字段中按下 Enter 键时)。

回答by Remy

Only thing you need to change is in your validateUser()function. IE is looking for the return value on the event, so you need to specify this:

你唯一需要改变的是你的validateUser()函数。IE 正在寻找事件的返回值,因此您需要指定以下内容:

event.returnValue = true;
return true;

event.returnValue = false;
return false;

回答by mrbinky3000

Please note that if there is a bug or error in validateUser() or deleteUser() then "return false" will NOT stop the anchor action and your browser will try to link to the href "subaction". Here is the logic:

请注意,如果 validateUser() 或 deleteUser() 中存在错误或错误,则“return false”不会停止锚点操作,您的浏览器将尝试链接到 href“子操作”。这是逻辑:

  1. User clicks on anchor
  2. onClick fires validateUser()
  3. There is an error in validateUser() so Javascript crashes and stops all code execution.
  4. return false is never fired because Javascript has already stopped.
  5. browser tries to go to href attribute
  1. 用户点击锚点
  2. onClick 触发 validateUser()
  3. validateUser() 中存在错误,因此 Javascript 崩溃并停止所有代码执行。
  4. return false 永远不会被触发,因为 Javascript 已经停止。
  5. 浏览器尝试转到 href 属性

If you are relying on a third party JavaScript API (I was using code supplied by Recyclebank to spawn a popup), and the third party API makes an update that breaks the JavaScript, then you'll have problems.

如果您依赖第三方 JavaScript API(我使用 Recyclebank 提供的代码来生成弹出窗口),并且第三方 API 进行了破坏 JavaScript 的更新,那么您就会遇到问题。

The following will stop the link under normal conditions and error conditions.

以下将在正常情况和错误情况下停止链接。

<a class="button" href="javascript:;" onclick="validateUser();return false;">Validate User</a>