v-model 之 雙向數(shù)據(jù)綁定

Vue 中使用v-model 指令來實(shí)現(xiàn)表單元素和數(shù)據(jù)的雙向綁定

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Document</title>
    <script type="text/javascript" src="vue1026.js"></script>
</head>
<body>
    <div id="app">
        <!-- value屬性只能將數(shù)據(jù)變化同步到視圖 -->
        <input type="text" :value="name">
        <!-- v-model系統(tǒng)指令能夠?qū)崿F(xiàn)雙向數(shù)據(jù)綁定,即不僅能將數(shù)據(jù)變化同步到視圖,還能將視圖上的變化同步到數(shù)據(jù) -->
        <input type="text" v-model="phone">
        <!-- 表單提交默認(rèn)方法是get可省略不寫,action="#"代表提交到當(dāng)前頁面 -->
        <form action="#">
            <input type="text" id="username" v-model="user.uname">
            <input type="password" id="pwd" v-model="user.upwd">
            <input type="button" v-on:click="submit" value="注冊(cè)">
        </form>
    </div>
</body>
<script type="text/javascript">
    var vm = new Vue({
        el:"#app",
        data:{
            name:"liy",
            phone:"123456",
            user:{
                uname:"",
                upwd:""
            }

        },
        methods:{
            submit:function(){
                alert("username=" + this.user.uname + "," + "password=" + this.user.upwd);
                console.log("username=" + this.user.uname + "," + "password=" + this.user.upwd);
            }
        }
    });
</script>
</html>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容