vue 验证码界面 点击后标灰并设置div按钮不可点击状态

1、先看看效果图

未点击获取验证码的按钮状态
在这里插入图片描述
点击后的不可点击状态
在这里插入图片描述

2、代码实现
<template>
	<div class="my-code">
		   <input class="my-code-input" type="text" v-model="login_form.captcha" placeholder="Your Captcha">
		   <div class="my-code-get" @click="get_captcha" id="new_yan">
		       <span v-show="show">Get Captcha</span>
		       <span v-show="!show">{{ count }} s</span>
		   </div>
	</div>
</template>

<script>
    import store from '@/store'
    import Vue from 'vue'
    import $ from 'jquery'

    export default {
        name: "register",
        data () {
            return {
                show: true,
                count: 60,
                timer: null,
            }
        },
        methods: {
            get_captcha() {
                 if (this.login_form.username === '' ) {
                    alert('Phone number or mailbox cannot be empty')
                } else {
                    if(this.timer == null){
                        getValidate(this.login_form.username).then(response => {
                            const data = response.data
                            console.log(data)
                            console.log('成功')
                        }).catch(error => {
                            console.log(error)
                            alert(error)
                        })
                    }  
                    if (!this.timer) {
                        this.count = 60;
                        this.show = false;
                        $(".my-code-get").addClass("huise")
                        // 将鼠标设置为不可点击状态
                        document.getElementById('new_yan').style.cursor = 'not-allowed'
                        this.timer = setInterval(() => {
                            if (this.count > 0 && this.count <= 60) {
                                this.count--
                            } else {
                                this.show = true
                                clearInterval(this.timer)
                                this.timer = null
                            }
                        }, 1000)
                    }
                }
            }
        },
        created: function() {
        },
        watch:{
            timer: function(val){
                console.log(val)
                if(val == null){
                     //  监听timer变化,移除不可点击样式
                    $(".my-code-get").removeClass("huise")
                    document.getElementById('new_yan').style.cursor = 'pointer'
                }
          }
        }
    }
</script>

<style scoped>
    .my-input{
        text-align: left;
        display: block;
        width: 400px;
        height: 35px;
        padding: 3px;
        margin: 20px calc(50% - 200px) 20px calc(50% - 200px);
        background:none;  
        outline:none;  
        border:0px;
        border-bottom: 2px solid #dcdcdc;
        border-bottom-left-radius: 1px;
        border-bottom-right-radius: 1px;
        box-sizing: border-box;
        font-family: PingFangSC-Regular;
        font-size: 16px;
    }
    .my-code{
        overflow: hidden;
    }
    .my-code-get{
        float: left;
        width: 120px;
        height: 35px;
        background-color: rgb(7, 187, 127);
        margin: 0 auto 20px 0;
        line-height: 35px;
        font-family: PingFangSC-Regular;
        color: #ffffff;
        border-radius: 5px;
        -webkit-user-select:none;
        -moz-user-select:none;
        -ms-user-select:none;
        user-select:none;
    }
    .my-code-get:active{
        background-color: #0F996B;
    }
    .my-code-get:hover{
        cursor: pointer;
    }
    .my-code-input{
        float: left;
        text-align: left;
        display: block;
        width: 280px;
        height: 35px;
        padding: 3px;
        margin: 0 auto 20px calc(50% - 200px);
        background:none;  
        outline:none;  
        border:0px;
        border-bottom: 2px solid #dcdcdc;
        border-bottom-left-radius: 1px;
        border-bottom-right-radius: 1px;
        box-sizing: border-box;
        font-family: PingFangSC-Regular;
        font-size: 16px;
    }
    .my-code-input:focus{
        border-bottom: 2px solid #0F996B;
        border-bottom-left-radius: 1px;
        border-bottom-right-radius: 1px;
    }
    .huise{
        background-color: #dcdcdc !important;
        color: black;
    }
</style>
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
你可以使用Vue的条件渲染和绑定属性来根据变量值设置div按钮的可点击状态。 首先,在你的Vue组件中,你需要定义一个变量来表示按钮的可点击状态。例如,你可以在data中定义一个名为`isButtonDisabled`的变量,初始值为`false`。 然后,你可以使用`v-bind`指令将这个变量绑定到按钮的`disabled`属性上,这样当`isButtonDisabled`的值为`true`时,按钮将变为不可点击状态。例如: ```html <template> <div> <button :disabled="isButtonDisabled">按钮</button> </div> </template> <script> export default { data() { return { isButtonDisabled: false }; } }; </script> ``` 接下来,你可以根据你的业务逻辑来动态修改`isButtonDisabled`的值,从而控制按钮的可点击状态。例如,当某个条件满足时,你可以将`isButtonDisabled`设为`true`,使按钮变为不可点击状态。例如: ```html <template> <div> <button :disabled="isButtonDisabled">按钮</button> <button @click="disableButton">禁用按钮</button> <button @click="enableButton">启用按钮</button> </div> </template> <script> export default { data() { return { isButtonDisabled: false }; }, methods: { disableButton() { this.isButtonDisabled = true; }, enableButton() { this.isButtonDisabled = false; } } }; </script> ``` 这样,当点击"禁用按钮"按钮时,按钮将变为不可点击状态;当点击"启用按钮"按钮时,按钮将恢复为可点击状态。 希望对你有帮助!如果有任何疑问,请随时追问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

haeasringnar

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值