如何用Vue模擬v-model


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <label>
            抽煙:<input type="checkbox" value="抽煙" @change="handleChange($event)"/>
        </label>
        <label>
            喝酒:<input type="checkbox" value="喝酒" @change="handleChange($event)"/>
        </label>
        <label>
            燙頭:<input type="checkbox" value="燙頭" @change="handleChange($event)"/>
        </label>
        <label>
            大保健:<input type="checkbox" value="大保健" @change="handleChange($event)"/>
        </label>
        <p v-for="item in checkVal">{{item}}</p>
    </div>
</body>
</html>
<script src="./vue.js"></script>
<script>
    new Vue({
        el:"#app",
        data:{
           checkVal:[]
        },
        methods:{
            handleChange(e){
                let val = e.target.value;
                
                let flag = this.checkVal.includes(val);
                if(flag){
                    var index = this.checkVal.indexOf(val);
                    this.checkVal.splice(index,1)
                }else{
                    this.checkVal.push(val);
                }
            }
        }
    })

   
</script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM