当前位置:  开发笔记 > 编程语言 > 正文

表单提交时禁用按钮

如何解决《表单提交时禁用按钮》经验,为你挑选了3个好方法。

我有一个按钮,我想在表单提交时禁用,以防止用户多次提交.

我已尝试使用javascript onclick天真地禁用该按钮,但如果客户端验证失败按钮仍保持禁用状态.

如果表单成功提交不仅仅是在用户单击时,如何禁用该按钮?

这是一个ASP.NET表单,所以如果可能的话,我想很好地与asp.net ajax页面生命周期挂钩.



1> Adam Nofsing..:

我不是在代码隐藏中编写所有javascript的忠实粉丝.这是我最终解决方案的样子.

按钮:


使用Javascript:




2> rp...:

给这个旋转:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

         // Identify button as a "disabled-when-clicked" button...
         WebHelpers.DisableButtonOnClick( buttonTest, "showPleaseWait" ); 
    }

    protected void buttonTest_Click( object sender, EventArgs e )
    {
        // Emulate a server-side process to demo the disabled button during
        // postback.
        Thread.Sleep( 5000 );
    }
}



using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Text;

public class WebHelpers
{
    //
    // Disable button with no secondary JavaScript function call.
    //
    public static void DisableButtonOnClick( Button ButtonControl )
    {
        DisableButtonOnClick( ButtonControl, string.Empty );    
    }

    //
    // Disable button with a JavaScript function call.
    //
    public static void DisableButtonOnClick( Button ButtonControl, string ClientFunction )
    {   
        StringBuilder sb = new StringBuilder( 128 );

        // If the page has ASP.NET validators on it, this code ensures the
        // page validates before continuing.
        sb.Append( "if ( typeof( Page_ClientValidate ) == 'function' ) { " );
        sb.Append( "if ( ! Page_ClientValidate() ) { return false; } } " );

        // Disable this button.
        sb.Append( "this.disabled = true;" ); 

        // If a secondary JavaScript function has been provided, and if it can be found,
        // call it. Note the name of the JavaScript function to call should be passed without
        // parens.
        if ( ! String.IsNullOrEmpty( ClientFunction ) ) 
        {
            sb.AppendFormat( "if ( typeof( {0} ) == 'function' ) {{ {0}() }};", ClientFunction );  
        }

        // GetPostBackEventReference() obtains a reference to a client-side script function 
        // that causes the server to post back to the page (ie this causes the server-side part 
        // of the "click" to be performed).
        sb.Append( ButtonControl.Page.ClientScript.GetPostBackEventReference( ButtonControl ) + ";" );

        // Add the JavaScript created a code to be executed when the button is clicked.
        ButtonControl.Attributes.Add( "onclick", sb.ToString() );
    }
}


完成客户端微妙所需的50行服务器端代码?不用了,谢谢!!
我不是一个骗子...但是要编写这么多代码而不是onsubmit ="this.button.disable = true"或像
标签那样的smth有点......很奇怪?:|

3> Turnkey..:

以下功能非常有用,无需使用往往不可靠的禁用部分.只需使用"return check_submit();" 作为提交按钮的onclick处理程序的一部分.

还应该有一个隐藏字段来保存form_submitted初始值为0;



function check_submit (){
            if (document.Form1.form_submitted.value == 1){
                alert("Don't submit twice. Please wait.");
                return false;
            }
            else{
                document.Form1.form_submitted.value = 1;
                return true;
            }
            return false;
    }

推荐阅读
mobiledu2402852357
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有