javascript 的问题:返回 false 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5800720/
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
problem with javascript: return false is not working
提问by codeomnitrix
Hey there is a link in my program as shown and onclick it calls the function clearform as shown:
嘿,我的程序中有一个链接,如图所示,onclick 它调用函数 clearform,如图所示:
Html Code:
html代码:
<a class="button" href="Cancel" style="left: 55%;" onclick="clearForm()">Cancel</a>
JavaScript Code:
JavaScript 代码:
function clearForm(){
document.getElementById("subjectName").value = "";
return false;
}
return false is not working in this code. actually the first line of the function executed successfully but the return false was failed. I mean page is redirected to url "Cancel".
return false 在此代码中不起作用。实际上函数的第一行执行成功,但返回 false 失败。我的意思是页面被重定向到网址“取消”。
回答by Vijay
Change your code as
将您的代码更改为
<a class="button" href="Cancel" onclick="return clearForm()">Cancel</a>
回答by alex
Your problem is you need to return the Boolean.
你的问题是你需要返回布尔值。
But, drop all that...
但是,放下这一切......
Attach your event unobtrusively...
element.onclick = clearForm;
Use
preventDefault()
. It is the modern way of acheiving that.function clearForm(event) { event.preventDefault(); }
不显眼地附加您的活动...
element.onclick = clearForm;
使用
preventDefault()
. 这是实现这一目标的现代方式。function clearForm(event) { event.preventDefault(); }
回答by mrbinky3000
Please note that if there is a bug or error in clearForm() then "return false" will NOT stop the anchor action and your browser will try to link to the href "Cancel". Here is the logic:
请注意,如果 clearForm() 中存在错误或错误,则“return false”不会停止锚点操作,您的浏览器将尝试链接到 href“Cancel”。这是逻辑:
- User clicks on anchor
- onClick fires clearForm()
- There is an error in clearForm() so Javascript crashes and stops all code execution.
- return false is never fired because Javascript has already stopped.
- 用户点击锚点
- onClick 触发 clearForm()
- clearForm() 中存在错误,因此 Javascript 崩溃并停止所有代码执行。
- return false 永远不会被触发,因为 Javascript 已经停止。
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:;" style="left: 55%;" onclick="clearForm();return false;">Cancel</a>
回答by Flask
<a class="button" href="Cancel" style="left: 55%;" onclick="clearForm();return false;">Cancel</a>
should work
应该管用
回答by userup
Keep in mind that some browser extensions may interfere with proper operation of links. For example, I ran into a situation where someone had both AdBlock Plus and Ghostery enabled. Clicking a simple 'a' tag with an 'onclick="return SomeFunction();"' attribute (the function returned false) caused the browser to treat the click as a page transition and went to a blank page and the loading indicator just kept spinning. Disabling those browser extensions cleared up the problem.
请记住,某些浏览器扩展程序可能会干扰链接的正常运行。例如,我遇到了有人同时启用 AdBlock Plus 和 Ghostery 的情况。单击带有 'onclick="return SomeFunction();"' 属性(函数返回 false)的简单 'a' 标记会导致浏览器将单击视为页面转换并转到空白页面,并且加载指示器保持不变纺纱。禁用这些浏览器扩展可以解决问题。
Including this as an answer because this was the first page I landed on from Google.
将此作为答案,因为这是我从 Google 登陆的第一个页面。