javascript 问题:返回 false 不起作用

发布于 2024-11-03 20:38:24 字数 432 浏览 5 评论 0原文

嘿,我的程序中有一个链接,如图所示,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

2024-11-10 20:38:24

将您的代码更改为

<a class="button" href="Cancel" onclick="return clearForm()">Cancel</a>

Change your code as

<a class="button" href="Cancel" onclick="return clearForm()">Cancel</a>
帝王念 2024-11-10 20:38:24

你的问题是你需要返回布尔值。

但是,放弃所有这些...

  • 不引人注目地附加您的活动...

    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();
    }

一刻暧昧 2024-11-10 20:38:24
<a class="button" href="Cancel" style="left: 55%;" onclick="clearForm();return false;">Cancel</a>

应该有效

<a class="button" href="Cancel" style="left: 55%;" onclick="clearForm();return false;">Cancel</a>

should work

静若繁花 2024-11-10 20:38:24

请注意,如果clearForm() 中存在错误或错误,则“return false”将不会停止锚定操作,并且您的浏览器将尝试链接到href“取消”。逻辑如下:

  1. 用户单击锚点
  2. onClick 触发clearForm()
  3. clearForm() 中存在错误,因此 Javascript 崩溃并停止所有代码执行。
  4. return false 永远不会被触发,因为 Javascript 已经停止了。

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

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

<a class="button" href="javascript:;" style="left: 55%;" onclick="clearForm();return false;">Cancel</a>

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:

  1. User clicks on anchor
  2. onClick fires clearForm()
  3. There is an error in clearForm() so Javascript crashes and stops all code execution.
  4. return false is never fired because Javascript has already stopped.

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.

<a class="button" href="javascript:;" style="left: 55%;" onclick="clearForm();return false;">Cancel</a>
自由范儿 2024-11-10 20:38:24

返回 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()".

难得心□动 2024-11-10 20:38:24

参考这个问题:

JavaScript 确认取消按钮仍在发布

我遇到了同样的问题,并且还需要在用户单击提交表单时禁用提交按钮以防止重复提交。
这是我的代码,并且测试成功。

<script type="text/javascript">
        function confirmSubmit(event) {

            if (!confirm('Are you certain you want to lodge this form ?')) {
                event.disabled = false;

                event.preventDefault();
                stopPropagation();

                return false;
            }
            return true;
        }
    </script>

 <span class="btn">
        <asp:Button ID="btnSubmit" runat="server" Text="Lodge this form now" UseSubmitBehavior="false" OnClientClick="this.disabled='true';confirmSubmit(this)" OnClick="Submit"  />
    </span>

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.

<script type="text/javascript">
        function confirmSubmit(event) {

            if (!confirm('Are you certain you want to lodge this form ?')) {
                event.disabled = false;

                event.preventDefault();
                stopPropagation();

                return false;
            }
            return true;
        }
    </script>

 <span class="btn">
        <asp:Button ID="btnSubmit" runat="server" Text="Lodge this form now" UseSubmitBehavior="false" OnClientClick="this.disabled='true';confirmSubmit(this)" OnClick="Submit"  />
    </span>

断念 2024-11-10 20:38:24

请记住,某些浏览器扩展可能会干扰链接的正常运行。例如,我遇到过有人同时启用了 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文