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>