简体   繁体   中英

How to disable button on click event?

以下代码在IE中不起作用。我需要在Chrome,Firefox和IE中修复此问题

 button.Attributes["disabled"] = "disabled";

You just have to set it's Enabled property to false on serverside:

button.Enabled = false; 

Edit: If button is a link and it doesn't work in other browsers than IE, have a look at following link: http://www.aspnetajaxtutorials.com/2009/05/how-to-enable-or-disable-linkbutton-in.html

<asp:LinkButton ID="lnkTest" runat="server" CommandArgument="1" CommandName="1x"
OnClick="lnkTest_Click">Test</asp:LinkButton>

Style for enabling and Disabling is

<style>
.LnkEnabled
{
cursor: pointer;
}
.LnkDisabled
{
cursor: default;
color: Gray;
}
</style>

javascript function

<script language="javascript">
function EnableLinkButton(ID,flag)
{
   document.getElementById(ID).onclick=function(){return flag;};
   if(!flag)
   { 
      document.getElementById(ID).setAttribute("disabled","disabled");
      document.getElementById(ID).className="LnkDisabled";
   }
   else
   {
      document.getElementById(ID).setAttribute("disabled","");
      document.getElementById(ID).className="LnkEnabled";
   }
}
EnableLinkButton('<%= lnkTest.ClientID %>',false);
</script>

Edit2 : If that doesn't work (haven't tested it), you could also try this:

button.Attributes.Add("onClick", "return !this.disabled;")
 button.Attributes.Add("disabled", "true"); 
$('#button_id').attr('disabled', 'disabled');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM