具体页面代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" /></div>
<script type="text/javascript" language="javascript">
var secs = 10;
var wait = secs * 1000;
var agreetext="同 意";
document.form1.Button1.value = agreetext+"(" + secs + ") ";
document.form1.Button1.disabled = true;
for(i = 1; i <= secs; i++) {
window.setTimeout("update(" + i + ")", i * 1000);
}
window.setTimeout("timer()", wait);
function update(num, value) {
if(num == (wait/1000)) {
document.form1.Button1.value = agreetext;
} else {
printnr = (wait / 1000)-num;
document.form1.Button1.value = agreetext+"(" + printnr + ") ";
}
}
function timer() {
document.form1.Button1.disabled = false;
document.form1.Button1.value = agreetext;
}
</script>
</form>
</body>
</html>
注意以下两点:
1、setTimeout(表达式,延时时间)
在执行时,是在载入后延迟指定时间后,去执行一次表达式。
2、javascript必须放在<form>与</form>内。
3、在<form>与</form>内的JAVA脚本才能使用“document.form1.服务控件名”来引用页面中的服务类型的控件。如果将JAVA脚本放在<head>节内是不能使用这种形式来引用控件的。