如何禁用页面上的所有按钮和链接,但仍允许回发?

发布于 2024-09-12 13:56:24 字数 856 浏览 6 评论 0原文

我试图阻止用户单击页面上多个导致回发的元素。换句话说,如果他们单击“继续”提交按钮,他们不应该能够在原始请求返回之前导致另一次回发。

我尝试过两个版本的 jQuery 代码。两者都不完全符合我的要求:

此版本将禁用所有回发元素,但这样做会阻止单击的元素触发。根据记录,我不认为 .removeAttr('onclick') 确实是必需的,但将其排除似乎并没有改变行为。

$(function() {
    $('a, :button, :submit').click(function() {
        var elements = $('a, :button, :submit');
        elements.attr('disabled', 'disabled').removeAttr('onclick');
    });
});

此版本禁用所有其他回发元素,但它允许我重用被单击的同一元素 - 我不希望能够两次点击同一按钮。

$(function() {
    $('a, :button, :submit').click(function() {
        var otherelements = $('a:not(#' + $(this).attr('id') + '), :button:not(#' + $(this).attr('id') + '), :submit:not(#' + $(this).attr('id') + ')');
        elements.attr('disabled', 'disabled').removeAttr('onclick');
    });
});

我怎样才能防止这种行为?

I'm trying to prevent the user from clicking on more than one postback-causing element on the page. In other words, if they click the 'continue' submit button, they shouldn't be able to cause another postback before the original request comes back.

I've had a go with two versions of jQuery code. Neither does exactly what I want:

This version will disable all the postback elements, but in doing so, it stops the clicked element from firing. For the record, I don't think the .removeAttr('onclick') is really required, but leaving it out doesn't seem to change the behaviour.

$(function() {
    $('a, :button, :submit').click(function() {
        var elements = $('a, :button, :submit');
        elements.attr('disabled', 'disabled').removeAttr('onclick');
    });
});

This version disables all other postback elements, but it lets me reuse the same element that was clicked - I don't want to be able to hit the same button twice.

$(function() {
    $('a, :button, :submit').click(function() {
        var otherelements = $('a:not(#' + $(this).attr('id') + '), :button:not(#' + $(this).attr('id') + '), :submit:not(#' + $(this).attr('id') + ')');
        elements.attr('disabled', 'disabled').removeAttr('onclick');
    });
});

How can I prevent this behaviour?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

怎会甘心 2024-09-19 13:56:24

我刚刚在没有 JQuery 的情况下测试了你的第一种方法,它工作得很好,即禁用提交按钮并没有阻止表单提交。

<form method="get">
  <input type="text" name="textfield" value="a" />
  <input type="submit" onclick="this.disabled=true">
</form>

也许您想仔细检查是否还有其他情况,例如 JQuery,正在发生?

I just tested your first approach without JQuery, and it worked fine, i.e. disabling the submit button didn't prevent the form submission.

<form method="get">
  <input type="text" name="textfield" value="a" />
  <input type="submit" onclick="this.disabled=true">
</form>

Maybe you want to double check is there is anything else, e.g. JQuery, going on?

影子是时光的心 2024-09-19 13:56:24

也许您可以放置​​一个标志或其他东西,让它记住单击了哪个按钮,如果该标志存在,您可以删除该回发元素上的 onclick 事件。但我认为这不能单独在客户端脚本中完成,因为一旦提交页面,所有客户端元素和脚本都会刷新。

Maybe you could put a flag or something that it could remember what button it was clicked and if that flag exist, you can remove the onclick event on that postback-causing element. But I think this cannot be done in client side scripting alone, since once the page is submitted, all client side elements and scripts are refreshed.

蝶…霜飞 2024-09-19 13:56:24

也许不要将其设置为单击函数,而是将其设置为 onmouseup,以便它在单击事件发生后触发。

Perhaps instead of making this a click function make it onmouseup so it fires after the click event has occured.

七七 2024-09-19 13:56:24

这是一个有效的最终版本 - 只是覆盖表单提交事件而不是查看任何单独的元素。

var submitted = false;
$(function() {
    $('form').bind('submit', function() {
        if (!submitted) {
            submitted = true;
            return true;
        } else {
            return false;
        }
    });
});

感谢大家的建议。

Here's a final version that worked - just overriding the form submit event rather than looking at any individual elements.

var submitted = false;
$(function() {
    $('form').bind('submit', function() {
        if (!submitted) {
            submitted = true;
            return true;
        } else {
            return false;
        }
    });
});

Thanks all for your suggestions.

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