asp.net下按鈕點(diǎn)擊后禁用的實(shí)現(xiàn)代碼
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="DisableButton.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript">
function enableButton(flag) {
$("#btnTest").attr("disabled", flag? "" : "disabled");
}
$(document).ready(
function () {
$("#btnTest").click(
function () {
enableButton( false );//點(diǎn)擊后禁用
}
);
}
);
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnTest" Text="點(diǎn)擊后禁用" runat="server" OnClick="Test" />
</div>
</form>
</body>
</html>
然而事實(shí)很遺憾的告訴我們這種方式行不通:頁(yè)面根本不會(huì)回發(fā)。于是,我們不得不尋找其他方式。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="DisableButton.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript">
function enableButton(flag) {
$("#btnTest").attr("disabled", flag? "" : "disabled");
}
$(document).ready(
function () {
$("#btnTest").click(
function () {
enableButton(false);
$("#btnTest2").click();//禁用掉自身并調(diào)用真正觸發(fā)回發(fā)的按鈕的click事件
}
);
}
);
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="點(diǎn)擊后禁用" id="btnTest" />
<asp:Button ID="btnTest2" Text="點(diǎn)擊后禁用" runat="server" OnClick="Test" style="display:none"/>
</div>
</form>
</body>
</html>
這樣一來(lái)我們的目的達(dá)到了。最后再介紹一種方式:三、利用setTimeout實(shí)現(xiàn)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="DisableButton.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript">
function enableButton(flag) {
$("#btnTest").attr("disabled", flag? "" : "disabled");
}
$(document).ready(
function () {
$("#btnTest").click(
function () {
setTimeout(function () {
enableButton(false);
},
50);
}
);
}
);
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnTest" Text="點(diǎn)擊后禁用" runat="server" OnClick="Test"/>
</div>
</form>
</body>
</html>
這樣不用引入輔助控件我們也實(shí)現(xiàn)了需求。
注:為了更好的觀察試驗(yàn)效果,可以在按鈕的Click時(shí)間處理函數(shù)中Sleep幾秒。
當(dāng)然可以使用 jquery 的 unbind 與 bind 函數(shù)實(shí)現(xiàn)對(duì)它的click 事件移除或者添加操作.
相關(guān)文章
ASP.NET Core Web App應(yīng)用第三方Bootstrap模板的方法教程
這篇文章主要給大家介紹了關(guān)于ASP.NET Core Web App應(yīng)用第三方Bootstrap模板的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起看看吧2018-06-06
Could not load file or assembly "App_Licenses.dll"
Could not load file or assembly "App_Licenses.dll"的問(wèn)題2010-03-03
完美兼容ie和firefox的asp.net網(wǎng)站加入收藏和設(shè)置主頁(yè)
這篇文章主要介紹了完美兼容ie和firefox的asp.net網(wǎng)站加入收藏和設(shè)置主頁(yè),需要的朋友可以參考下2014-12-12
.NET做人臉識(shí)別并分類(lèi)的實(shí)現(xiàn)示例
這篇文章主要介紹了.NET做人臉識(shí)別并分類(lèi)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
asp.net中DBNull.Value,null,String.Empty區(qū)別淺析
這篇文章來(lái)給大家介紹asp.net中DBNull.Value,null,String.Empty區(qū)別淺析,有需要的同學(xué)可以參考一下2013-08-08
asp.net session的使用與過(guò)期實(shí)例代碼
本文章來(lái)簡(jiǎn)單的介紹asp.net中session常見(jiàn)兩種用法,一種是session使用如何創(chuàng)建,另一種是告訴你如何判斷session過(guò)期了,有需要了解的朋友可以參考一下2013-08-08
解決Asp.net Mvc返回JsonResult中DateTime類(lèi)型數(shù)據(jù)格式問(wèn)題的方法
這篇文章主要介紹了解決Asp.net Mvc返回JsonResult中DateTime類(lèi)型數(shù)據(jù)格式問(wèn)題的方法,需要的朋友可以參考下2016-06-06
ASP.net?core使用Autofac實(shí)現(xiàn)泛型依賴(lài)注入
這篇文章主要介紹了ASP.net?core使用Autofac實(shí)現(xiàn)泛型依賴(lài)注入的方式學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
Asp.net Web Api實(shí)現(xiàn)圖片點(diǎn)擊式圖片驗(yàn)證碼功能
現(xiàn)在驗(yàn)證碼的形式越來(lái)越豐富,今天要實(shí)現(xiàn)的是在點(diǎn)擊圖片中的文字來(lái)進(jìn)行校驗(yàn)的驗(yàn)證碼。下面通過(guò)本文給大家分享Asp.net Web Api實(shí)現(xiàn)圖片點(diǎn)擊式圖片驗(yàn)證碼功能,需要的的朋友參考下吧2017-06-06

