如何禁用页面上的所有按钮和链接,但仍然让回发发生? [英] How can I disable all buttons and links on my page, but still let the postback occur?

查看:121
本文介绍了如何禁用页面上的所有按钮和链接,但仍然让回发发生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图防止用户单击页面上的多个导致回发的元素.换句话说,如果他们单击继续"提交按钮,则他们将不能在原始请求返回之前引起另一次回发.

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.

我已经使用了两个版本的jQuery代码.也不是我想要的:

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

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

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

如何防止这种行为?

推荐答案

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

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

感谢您的建议.

这篇关于如何禁用页面上的所有按钮和链接,但仍然让回发发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆