赞
踩
Vue.js可以很方便的实现数据双向绑定,所以在处理表单,人机交互方面具有很大的优势。下边以单选框、复选框和下拉框为例介绍他们在HTML和Vue.js中的具体实现方式。
- <div id="app">
- <input type="radio" name="gender" value="man" id="man"/><label for="man">男</label>
- <input type="radio" name="gender" value="woman" id="women"/><label for="women">女</label>
- </div>
- <div id="app">
- <label>男<input type="radio" v-model="gender" value="man"/></label>
- <label>女<input type="radio" v-model="gender" value="woman"/></label>
- <p>已选:{{gender}}</p><!--如果用原生js实现此功能比较麻烦-->
- </div>
- <script>
- var app=new Vue({
- el:'#app',
- data:{
- gender:''
- }
- });
- </script>
- <div id="app">
- <input type="checkbox" name="whom" value="jack" id="Jack"/><label for="Jack">jack</label>
- <input type="checkbox" name="whom" value="bob" id="Bob"/><label for="Bob">bob</label>
- <input type="checkbox" name="whom" value="alice" id="Alice"/><label for="Alice">alice</label>
- </div>
- <div id="app">
- <label>jack<input type="checkbox" v-model="person.jack"/></label>
- <label>bob<input type="checkbox" v-model="person.bob"/></label>
- <label>alice <input type="checkbox" v-model="person.alice"/></label>
- <p>已选:{{person}}</p>
- </div>
- <script>
- var app = new Vue({
- el: '#app',
- data: {
- person: {jack: false, bob: false, alice: false}
- }
- })
- </script>
- <div id="app">
- <label>jack<input type="checkbox" v-model="whom" value="jack"/></label>
- <label>bob<input type="checkbox" v-model="whom" value="bob"/></label>
- <label>alice <input type="checkbox" v-model="whom" value="alice"/></label>
- <p>已选:{{whom.join('|')}}</p>
- </div>
- <script>
- var app = new Vue({
- el: '#app',
- data: {
- whom: []
- }
- })
- </script>
- <select name="selected">
- <option value="a">A</option>
- <option value="b">B</option>
- <option value="c">C</option>
- </select>
- <div id="app">
- <select v-model="selected">
- <option v-for="item in items" v-bind:value="item.value">{{item.text}}</option>
- </select>
- <span>已选:{{selected}}</span>
- </div>
- <script src="vue.js"></script>
- <script>
- new Vue({
- el:'#app',
- data:{
- items:[{text:'A',value:'a'},{text:'B',value:'b'},{text:'C',value:'c'}],
- selected:''
- }
- });
- </script>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。