jQuery 倒计时状态显示在按钮中

标签 jquery html

在启用按钮之前,我使用以下代码进行倒计时。

<script type="text/javascript">
$.fn.timedDisable = function(time) {
    if (time == null) { time = 5000; }
    return $(this).each(function() {
        $(this).attr('disabled', 'disabled');
        var disabledElem = $(this);
        setTimeout(function() {
            disabledElem.removeAttr('disabled');
        }, time);
    });
};

$(function() {
    $('#btnContinue').timedDisable();
});
</script>

如何获取按钮值以读取 value="Continue (x)", 其中 x 是启用之前剩余的秒数,之后它只是 value="Continue"?

最佳答案

这个怎么样:

示例: http://jsfiddle.net/mgSVX/2/ 编辑:(更新示例以使用请求的文本值)

$.fn.timedDisable = function(time) {
    if (time == null) {
        time = 5000;
    }
    var seconds = Math.ceil(time / 1000);  // Calculate the number of seconds
    return $(this).each(function() {
        $(this).attr('disabled', 'disabled');
        var disabledElem = $(this);
        var originalText = this.innerHTML;  // Remember the original text content

          // append the number of seconds to the text
        disabledElem.text( originalText + ' (' + seconds + ')'); 

         // do a set interval, using an interval of 1000 milliseconds
         //     and clear it after the number of seconds counts down to 0
        var interval = setInterval(function() {
                // decrement the seconds and update the text
            disabledElem.text( originalText + ' (' + seconds + ')');  
            if (seconds === 0) {  // once seconds is 0...
                disabledElem.removeAttr('disabled')
                    .text(originalText);   //reset to original text
                clearInterval(interval);  // clear interval
            }
        }, 1000);
    });
};

$(function() {
    $('#btnContinue').timedDisable();
});​

关于jQuery 倒计时状态显示在按钮中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3773582/

相关文章:

javascript - 在图像上定位图像

java - 如何处理 Java 中的字符集差异

html - 如何将日期选择器添加到引导页面?

html - 让我的按钮左右而不是堆叠?

javascript - 将 img HTML 标记与对 C# 函数的调用相结合添加到 JavaScript 脚本内的 ASP.NET 页面

jquery - jqGrid 上的 prettyCheckable 不起作用

javascript - 如何使用 Javascript/JQuery 在悬停时设置缩略图背景?

javascript - 如何使用jquery检测文本以特殊单词开头的元素数量?

jquery - 过渡不在 CSS3 中的旋转元素上运行

php - 将 HTML\PHP 页面加载到另一个页面的 div 中