javascript 问题:返回 false 不起作用
嘿,我的程序中有一个链接,如图所示,onclick 它调用函数clearform,如下所示:
Html 代码:
<a class="button" href="Cancel" style="left: 55%;" onclick="clearForm()">Cancel</a>
JavaScript 代码:
function clearForm(){
document.getElementById("subjectName").value = "";
return false;
}
return false 在此代码中不起作用。实际上函数的第一行执行成功但返回 false 失败。我的意思是页面被重定向到 url“取消”。
Hey there is a link in my program as shown and onclick it calls the function clearform as shown:
Html Code:
<a class="button" href="Cancel" style="left: 55%;" onclick="clearForm()">Cancel</a>
JavaScript Code:
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".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
将您的代码更改为
Change your code as
你的问题是你需要返回布尔值。
但是,放弃所有这些...
不引人注目地附加您的活动...
element.onclick = clearForm;
使用
preventDefault()
。这是实现这一目标的现代方式。函数clearForm(事件) {
event.preventDefault();
}
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();
}
应该有效
should work
请注意,如果clearForm() 中存在错误或错误,则“return false”将不会停止锚定操作,并且您的浏览器将尝试链接到href“取消”。逻辑如下:
如果您依赖第三方 JavaScript API(我使用 Recyclebank 提供的代码来生成弹出窗口),并且第三方 API 进行的更新破坏了 JavaScript,那么您就会遇到问题。
以下将在正常情况和错误情况下停止链接。
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:
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.
The following will stop the link under normal conditions and error conditions.
返回 false
;不知何故需要在前面。(在过去几个月我处理过的所有情况下 - 可能是也可能不是错误)。
像这样:
onclick="return false;clearForm();"
除此之外,正如其他人提到的,您需要从 onclick 返回它,而不仅仅是从函数中返回它。< /强>
在您的情况下:
onclick="return clearForm()"
。The
return false
; somehow needs to be right at the front.(In ALL situations I've dealt with over the past months - may or may not be a bug).
Like this:
onclick="return false; clearForm();"
Besides that, as mentioned by others as well, you need to return it from the onclick, not just from the function.
In your case:
onclick="return clearForm()"
.参考这个问题:
JavaScript 确认取消按钮仍在发布
我遇到了同样的问题,并且还需要在用户单击提交表单时禁用提交按钮以防止重复提交。
这是我的代码,并且测试成功。
Referring to this question:
JavaScript confirm cancel button still posting
I had the same issue, and also need to disable submit button when user click submit the form to prevent double submission.
This is my code, and tested successfully.
请记住,某些浏览器扩展可能会干扰链接的正常运行。例如,我遇到过有人同时启用了 AdBlock Plus 和 Ghostery 的情况。单击带有 'onclick="return SomeFunction();"' 属性(函数返回 false)的简单 'a' 标签会导致浏览器将单击视为页面转换并转到空白页面,并且加载指示器保持不变旋转。禁用这些浏览器扩展可以解决问题。
将此作为答案,因为这是我从 Google 登陆的第一个页面。
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.
Including this as an answer because this was the first page I landed on from Google.