Web单击按钮后使按钮不可用并在6秒后设置其为可用JS实现

需求如题:

代码如下:

<script language="javascript">
function disableButton(button) 
{ 
    var btn =button;
    btn.disabled = true; 
    setTimeout(function(){disable_in(btn);},6000);
} 
function disable_in(button)
{
    button.disabled=false;
}
</script>

调用如下:

<asp:Button ID="btdownload" runat="server" Text="Download All" Width="200px" UseSubmitBehavior="false" OnClientClick="disableButton(this)"/>

在绑定控件中调用如下:

<asp:DataList ID="ImgItemdlist" runat="server" RepeatColumns="5"  
            RepeatDirection="Horizontal">
             <ItemTemplate>
            <asp:Button ID="btImage" runat="server" Text='<%#Eval("btImgName") %>' CommandName = "downloadImage" CommandArgument ='<%#Eval("btImgFileName")%>' Enabled='<%#Eval("btEnable") %>' UseSubmitBehavior="false" OnClientClick="disableButton(this)"/>
        </ItemTemplate>
        </asp:DataList>

注意:使用时要添加属性UseSubmitBehavior="false", 否则其不会触发服务器端的click事件。

在调用setTimeout调用带参函数时, 需要通过匿名函数调用带参函数。

原文地址:https://www.cnblogs.com/tylertang/p/3117183.html