定時設定按鈕可用性,防止不間斷提交

來源:互聯網
上載者:User

如有不明白的地方歡迎加QQ群14670545 探討

18位長度的計時周期數: DateTime.Now.Ticks.ToString()

<script type="text/javascript">//$j = jQuery.noConflict();var time_begin = new Date();var settime=3;var i;var showthis;$(document).ready(function() {    for(i=1;i<=settime;i++) {        setTimeout("update("+i+")",i*1000);    }});function update(num) {    if(num==settime) {        $("#next_button").attr("disabled",false);        $("#surplus_time").text("");    }    else {        showthis=settime-num;        $("#surplus_time").text(showthis);        $("#next_button").attr("disabled",true);    }}</script>

下面來一個具體的例子:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="pattern_memo.aspx.cs" Inherits="MyWebSiteTest.DesignPatterns.pattern_memo" %><!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 src="../Js/jquery-1.4.2.min.js" type="text/javascript"></script>    <script type="text/javascript">        //$j = jQuery.noConflict();          var time_begin = new Date();        var settime = 4;        var i;        var showthis;        $(document).ready(function () {            $("#next_button").attr("disabled", true);            for (i = 1; i <= settime; i++) {                setTimeout("update(" + i + ")", i * 1000);            }        });        function update(num) {            if (num == settime) {                $("#next_button").attr("disabled", false);                $("#surplus_time").text("");            }            else {                showthis = settime - num;                $("#surplus_time").text(showthis);                $("#next_button").attr("disabled", true);            }        }  </script>  </head><body>    <form id="form1" runat="server">    <div>        <span id="surplus_time"></span><br />        name:<asp:TextBox runat="server" ID="txtname" /><br />         pwd:<asp:TextBox runat="server" ID="txtpwd" /><br />        <asp:Button runat="server" ID="next_button" Text="提交" OnClientClick="return checkinput();" onclick="next_button_Click" />    </div>    </form>    <script type="text/javascript">        function checkinput() {            var _name = document.getElementById("txtname");            var _pwd = document.getElementById("txtpwd");            if ($.trim(_name.value) == "") {                $("#surplus_time").text("please write your name");                _name.focus();                return false;            }            if ($.trim(_pwd.value) == "") {                $("#surplus_time").text("please write your pwd");                _pwd.focus();                return false;            }            return true;        }    </script></body></html>
public partial class pattern_memo : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {        }        protected void next_button_Click(object sender, EventArgs e)        {        }    }

注意了,在頁面的ready之後一定要加上一句

$("#next_button").attr("disabled", true);

否則會有簡短的一秒左右button按鈕是可用狀態的,而如果你直接在button按鈕上面添加Enabled(disabled)屬性,則OnClientClick方法將不會奏效。

看看效果比較:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.