文本框绑定: 在此绑定中,我们使用文本和textarea绑定value属性和输入事件。
单选框/选择框绑定: : 在此绑定中,我们使用复选框和单选按钮绑定被选中的属性并更改事件。
修饰符绑定: 在表单输入绑定示例中,我们也可以使用.lazy,.trim,.number等修饰符。
<html>
<head>
<title>Vue.js Data Binding</title>
<link rel="stylesheet" href="index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id = "databinding">
<h3>Textbox</h3>
<input v-model = "name" placeholder = "Enter your name.." />
<h3>Your name is: {{name}}</h3>
<hr/>
<h3>Textarea</h3>
<textarea v-model = "textmessage" placeholder = "Add Details"></textarea>
<h1><p>{{textmessage}}</p></h1>
<hr/>
<h3>Checkbox</h3>
<input type = "checkbox" id = "checkbox" v-model = "checked"> {{checked}}
</div>
<script src="index.js"></script>
</body>
</html>
var vm = new Vue({
el: '#databinding',
data: {
name:'',
textmessage:'',
checked : false
}
})
html, body {
margin: 5px;
padding: 0;
}
<html>
<head>
<title>Vue.js Data Binding</title>
<link rel="stylesheet" href="index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id = "databinding">
<h3>Radio</h3>
<input type = "radio" id = "yes" value = "Yes" v-model = "picked"> Yes
<input type = "radio" id = "no" value = "No" v-model = "picked"> No
<h3>Radio element clicked: {{picked}} </h3>
<hr/>
<h3>Select</h3>
<select v-model = "languages">
<option disabled value = "">Please select one</option>
<option>Java</option>
<option>PHP</option>
<option>C</option>
<option>C++</option>
<option>JavaScript</option>
</select>
<h3>Your selected language is: {{ languages }}</h3>
<hr/>
</div>
<script src="index.js"></script>
</body>
</html>
var vm = new Vue({
el: '#databinding',
data: {
picked : 'No',
languages : "Java"
}
})
<input v-model.number = "age" type = "number">
<input v-model.lazy = "msg">
<input v-model.trim = "message">
<html>
<head>
<title>Vue.js Data Binding</title>
<link rel="stylesheet" href="index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id = "databinding">
<h3> The .number modifier example</h3>
<span style = "font-size:20px;">Enter Age:</span> <input v-model.number = "age" type = "number">
<br/>
<br/>
<hr/>
<h3> The .lazy modifier example</h3>
<span style = "font-size:20px;">Enter Message:</span> <input v-model.lazy = "msg">
<h4>Display Message : {{msg}}</h4>
<br/>
<hr/>
<h3> The .trim modifier example</h3>
<span style = "font-size:20px;">Enter Message : </span><input v-model.trim = "message">
<h4>Display Message : {{message}}</h4>
</div>
<script src="index.js"></script>
</body>
</html>
var vm = new Vue({
el: '#databinding',
data: {
age : 0,
msg: '',
message : ''
}
})