我有一个ASP.NET按钮,我需要在用户点击后禁用它,以防止重复点击。一旦提交完成,它必须再次启用。有人可以帮我吗?
如果您使用的是asp.net验证器,则此解决方案适用:
<script language="javascript" type="text/javascript">
function disableButton(sender,group)
{
Page_ClientValidate(group);
if (Page_IsValid)
{
sender.disabled = "disabled";
__doPostBack(sender.name, '');
}
}</script>
并更改按钮:
<asp:Button runat="server" ID="btnSendMessage" Text="Send" OnClick="btnSendMessage_OnClick" OnClientClick="disableButton(this,'theValidationGroup')" CausesValidation="true" ValidationGroup="theValidationGroup" />
使用jQuery:
$('form').submit(function(){
$(':submit', this).click(function() {
return false;
});
});
它对我有效。
<asp:Button ID="button" runat="server" CssClass="normalButton"
Text="Button" OnClick="Button_Click" ClientIDMode="Static"
OnClientClick="this.disabled = true; setTimeout('enableButton()', 1500);"
UseSubmitBehavior="False"/>
<script type="text/javascript">
function enableButton() {
document.getElementById('button').disabled = false;
}
</script>
getElementById('button')
?难道不应该是 <%= button.ClientID %> 吗? - Kiquenet这适用于普通的HTML按钮。
<input id="Button1"
onclick="this.disabled='true';" type="button"
value="Submit" name="Button1"
runat="server" onserverclick="Button1_Click">
在回发完成之前,按钮将被禁用,以防止双击。
<script type = "text/javascript">
function DisableButtons() {
var inputs = document.getElementsByTagName("INPUT");
for (var i in inputs) {
if (inputs[i].type == "button" || inputs[i].type == "submit") {
inputs[i].disabled = true;
}
}
}
window.onbeforeunload = DisableButtons;
</script>
form
标签中,加入 onsubmit="javascript:this.getElementById("submitButton").disabled='true';"
。