Vue.js中的v-model双向数据绑定使用起来很方便。在用户操作控件时提取数据,并可以根据程序处理后的数据实时更新控件。
练习代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vue.js-表单输入绑定</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<style>
.table {
border: 1px solid #ddd;
border-collapse: collapse;
}
.table tr td{
text-align:center;
border:1px rgb(23, 23, 61) solid;
border-collapse: collapse;
}
</style>
</head>
<body>
<div id="demo">
<h2>Vue.js中的表单控件的绑定</h2>
<table class="table">
<tr>
<td>序号</td><td>控件类型</td><td>数据同步</td><td>说明</td>
</tr>
<tr>
<td>1</td><td><input type="text" placeholder="请输入内容" v-model="singleLineText"></td><td><div>{{singleLineText}}</div></td><td>单行文本框</td>
</tr>
<tr>
<td>2</td><td><textarea cols="22" rows="3" v-model="multiLineText" placeholder="请输入内容"></textarea></td><td><div>{{multiLineText}}</div></td><td>多行文本框</td>
</tr>
<tr>
<td>3</td>
<td>
<input type="radio" v-model="radioText" id="d1" value="是"><label for="d1">是</label>
<input type="radio" v-model="radioText" id="d2" value="否"><label for="d2">否</label>
</td>
<td><div>{{radioText}}</div></td><td>单选</td>
</tr>
<tr>
<td>4</td><td><input type="checkbox" v-model="singleSelectText"></td><td><div>{{singleSelectText}}</div></td><td>单选框</td>
</tr>
<tr>
<td>5</td>
<td>
<input type="checkbox" v-model="multiSelectText" id="c1" value="语文"><label for="c1">语文</label>
<input type="checkbox" v-model="multiSelectText" id="c2" value="数学"><label for="c2">数学</label>
<input type="checkbox" v-model="multiSelectText" id="c3" value="英语"><label for="c3">英语</label>
</td>
<td><div>{{multiSelectText}}</div></td><td>多选框</td>
</tr>
<tr>
<td>6</td>
<td>
<select v-model="dropDownselectBox">
<option>语文</option>
<option>数学</option>
<option>英语</option>
</select>
</td>
<td><div>{{dropDownselectBox}}</div></td><td>下拉单选框</td>
</tr>
<tr>
<td>7</td>
<td>
<select v-model="multiSelectBox" multiple>
<option>学习语文</option>
<option>学习数学</option>
<option>学习英语</option>
</select>
</td>
<td><div>{{multiSelectBox}}</div></td><td>多选框<br>(按ctrl键多选)</td>
</tr>
</table>
</div>
<script>
//数据
const VueDataObj={
singleLineText:'',
multiLineText:'',
singleSelectText:true,
multiSelectText:['语文'],
radioText:'是',
dropDownselectBox:'语文',
multiSelectBox:['学习语文'],
};
//方法
var methods={};
//计算属性
var computed={};
//监听
var watch={};
//Vue应用对象
var vm=new Vue({
el:'#demo',
data:VueDataObj,
methods,
computed,
watch,
})
</script>
</body>
</html>
页面:
?
|