美文网首页
设置按钮状态的两种思路

设置按钮状态的两种思路

作者: hugoren | 来源:发表于2018-11-22 17:20 被阅读0次

思路

这里的状态是指按钮的是否可用
不可用时变灰

image.png

1.根据交互结果设置

// 点击按钮后,设置按钮不可用              document.getElementById("submit").setAttribute("disabled", true);
        
$.ajax({
                  url: '',
                  type: 'get',
                  contentType: 'application/json; charset=UTF-8',
                  processData: true,
                  data: data,
                  success: function (data) {
// 有结果返回,再显示按钮可用                         document.getElementById("submit").removeAttribute("disabled");
                         if (data.retcode == '0') {
                             refresh_cols(data.cols, data.rows)
                             sub_table(data.jsonpc)
                         }
                         else {
                             refresh_cols([],[])
                             Toastr('message', data.stderr, 'warning')
                         }
                    },
                    error: function (data) {
                        document.getElementById("submit").removeAttribute("disabled");
                        Toastr('message', data, 'error')
                    }
             });

2.根据时间倒计时设置,以下是三种设置方式

1.遇到阅读协议时的倒计时

<!--html 倒计时确定 -->
<input type="button" id="bt" value="请仔细阅读协议(5)" disabled="true" />

<script type="text/javascript">
    var time = 5, //计数器
     timer=null;        //定时器
    window.onload = function(){
        var bt = document.getElementById('bt')
        timer = setInterval("daojishi()", 1000);//执行方法
    }
    function daojishi() {
        if(time <= 1) {
            bt.value = "确定";
            bt.removeAttribute("disabled");//删除按钮disabled属性
            clearInterval(timer);//清除定时器
            return;
        }else{
            time--;
            bt.value = ("请仔细阅读协议(" + time +")");
    }
}

</script>

第二种获取验证码,传参的方式,这个方法没有点击延迟

<!-- 获取验证码 -->
<input type="button" value="免费获取验证码" onclick="settime(this)"/> 

<script>
    var countdown=5;//计数器
    function settime(val) {//方法里面传参数
        if (countdown == 0) {
            val.removeAttribute("disabled");//指向按钮本身
            val.value="免费获取验证码";
            countdown=5;
        } else {
            val.setAttribute("disabled", true);
            val.value="重新发送(" + countdown + ")";
            countdown--;
            setTimeout(function() {
                settime(val)
            },1000)
        }
    }
</script>

第三章普通的方法,这种方法点击有延迟

<input type="button" id="btn" value="获取验证码"/>

<script>
    var count=5,
    timer2 = null;
    var btn = document.getElementById("btn")
    btn.onclick = function(){
    timer2=setInterval(function(){
        if (count==0) {
            clearInterval(timer2)
            btn.removeAttribute("disabled");
            btn.value="免费获取验证码";
            count=5;
        }else{
            btn.value = "重新发送(" + count + ")";
            count--;
            btn.setAttribute("disabled",true)
        }
    },1000)
}
</script>

参考:
https://www.jianshu.com/p/57767cad729f

相关文章

网友评论

      本文标题:设置按钮状态的两种思路

      本文链接:https://www.haomeiwen.com/subject/neilqqtx.html