这篇文章主要介绍“vue中如何利用v-model绑定表单控件”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“vue中如何利用v-model绑定表单控件”文章能帮助大家解决问题。
1、v-model 双向绑定文本
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="vue.js"></script> </head> <body> <div id="app"> <input v-model="message" placeholder="edit me"> <p>Message is: {{ message }}</p> </div> </body> <script> var vm = new Vue({ el:"#app", data: { message: '绑定文本' } }) </script> </html>
输出结果:
2、v-model 双向绑定多行文本,与上面的例子相似。
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="vue.js"></script> </head> <body> <div id="app"> <<span>Multiline message is:</span> <p >{{ message }}</p> <br> <textarea v-model="message" placeholder="add multiple lines"></textarea> </div> </body> <script> var vm = new Vue({ el:"#app", data: { message: '绑定多行文本' } }) </script> </html>
输出结果:
3、v-model 绑定复选框
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="vue.js"></script> </head> <body> <div id="app"> <input type="checkbox" id="checkbox" v-model="checked"> <label for="checkbox">{{ checked }}</label> </div> </body> <script> var vm = new Vue({ el:"#app", data: { checked: 'true' } }) </script> </html>
输出结果:选中为true 不选中则为false
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="vue.js"></script> </head> <body> <div id="app"> <input type="checkbox" id="jack" value="刘二狗" v-model="checkedNames"> <label for="jack">Jack</label> <input type="checkbox" id="john" value="张麻子" v-model="checkedNames"> <label for="john">John</label> <input type="checkbox" id="mike" value="小狗子" v-model="checkedNames"> <label for="mike">Mike</label> <br> <span>Checked names: {{ checkedNames }}</span> </div> </body> <script> var vm = new Vue({ el:"#app", data: { checkedNames: [] } }) </script> </html>
输出结果:
4、v-model 绑定单选按钮
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="vue.js"></script> </head> <body> <div id="app"> <input type="radio" id="one" value="One" v-model="picked"> <label for="one">One</label> <br> <input type="radio" id="two" value="Two" v-model="picked"> <label for="two">Two</label> <br> <span>Picked: {{ picked }}</span> </div> </body> <script> var vm = new Vue({ el:"#app", data: { picked: '' } }) </script> </html>
输出结果:
5、v-model 绑定下拉列表
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="vue.js"></script> </head> <body> <div id="app"> <select v-model="selected"> <option>A</option> <option>B</option> <option>C</option> </select> <span>Selected: {{ selected }}</span> </div> </body> <script> var vm = new Vue({ el:"#app", data: { selected: '' } }) </script> </html>
输出结果:
多选列表
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="vue.js"></script> </head> <body> <div id="app"> <select v-model="selected" multiple > <option>A</option> <option>B</option> <option>C</option> </select> <br> <span>Selected: {{ selected }}</span> </div> </body> <script> var vm = new Vue({ el:"#app", data: { selected: [] } }) </script> </html>
输出结果:
6、动态选项,用 v-for 渲染:
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="vue.js"></script> </head> <body> <div id="app"> <select v-model="selected"> <option v-for="option in options" v-bind:value="option.value"> {{ option.text }} </option> </select> <span>Selected: {{ selected }}</span> </div> </body> <script> var vm = new Vue({ el:"#app", data: { selected: 'A', options: [ { text: 'One', value: 'A' }, { text: 'Two', value: 'B' }, { text: 'Three', value: 'C' } ] } }) </script> </html>
输出结果:
关于“vue中如何利用v-model绑定表单控件”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。