阻止用户在ASP.NET中双击 [英] Prevent user from double clicking in ASP.NET

查看:91
本文介绍了阻止用户在ASP.NET中双击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了以下两个脚本,以防止用户提交多个以下表单。在这两种情况下都会禁用提交按钮,但是表单没有被发送(放置了一个断点并且没有调用OnClick方法)

I have tried the following two scripts to prevent user from submitting the below form more than one. The submit button is being disabled in both cases, however the form is not being sent (placed a break point and the OnClick method is not being called)

按钮:

<asp:Button ID="BtnSubmit" runat="server" Text="Submit" onclick="BtnSubmit_Click" />

脚本:

<script type="text/javascript">        $("#BtnSubmit").click(function () {
        var button = $(this);
        button.prop('disabled', true);
        setTimeout(function () {
            button.prop('disabled', false);
        }, 5000);
    });</script>

$('form').on('submit', function (e) {
$('[type="submit"]').prop('disabled','disabled');
});


推荐答案

一个简单的想法是改变按钮的类型到按钮(而不是提交),而不是禁用它。这样,如果至少再次点击,它将不会提交表格。

One quick idea is to change the type of the button to "button" (instead of "submit"), instead of disabling it. That way, it won't submit the form if clicked again at least.

另一个想法来自: http://encosia.com/disable-a-button-control-during-postback/ -

Another other idea comes from here: http://encosia.com/disable-a-button-control-during-postback/ -

链接中的重要文字:


禁用提交按钮时出现的一个陷阱
客户端是它将取消浏览器的提交,从而取消
回发。将 UseSubmitBehavior 属性设置为 false 告诉.NET
注入必要的客户端脚本以激发回发,
而不是依赖浏览器的表单提交行为。

The one pitfall that comes with disabling a submit button on the client side is that it will cancel the browser’s submit, and thus the postback. Setting the UseSubmitBehavior property to false tells .NET to inject the necessary client script to fire the postback anyway, instead of relying on the browser’s form submission behavior.

然后,你的按钮看起来像这样: / p>

Following that, your button would look something like this:

<asp:Button runat="server" ID="BtnSubmit" Text="Submit"
  OnClientClick="this.disabled = true; this.value = 'Submitting...';" 
  UseSubmitBehavior="false" 
  OnClick="BtnSubmit_Click" />

我不喜欢内联 OnClientClick ,所以你应该试着把它拿出来并保留你的jQuery点击处理程序,以及适当地设置 UseSubmitBehavior 。我觉得事件的顺序无关紧要。

I don't like the inline OnClientClick, so you should try taking it out and keeping your jQuery click handler, as well as setting the UseSubmitBehavior appropriately. I have a feeling the order of events shouldn't matter.

这篇关于阻止用户在ASP.NET中双击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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