1. 程式人生 > >vue輸入框限制字數,達到100字禁止輸入

vue輸入框限制字數,達到100字禁止輸入

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Examples</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
<script src="https://cdn.bootcss.com/vue/2.4.2/vue.min.js"></script>
</head>
<body>
    <div id="app">
        <input type="text" v-model="items.text" ref="count"/>
        <div  v-html="number"></div>
    </div>
    <script>
        new Vue({
            el: '#app',
            data: {
                number: '100',
                items: {
                    text:'',
                },
            },
            watch:{   //watch()監聽某個值(雙向繫結)的變化,從而達到change事件監聽的效果
                items:{
                    handler:function(){
                        var _this = this;
                        var _sum = 100; //字型限制為100個
                        _this.$refs.count.setAttribute("maxlength",_sum);
                        _this.number= _sum- _this.$refs.count.value.length;
                    },
                    deep:true
                }
            }
        })
    </script>
</body>
</html>