model表单控件绑定详解

v-model 指令在表单控件元素上创建双向数据绑定,下面一一进行示例解释。

1、v-model 双向绑定文本

<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="https://www.jb51.net/article/vue.js"></script> </head> <body> <div> <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="https://www.jb51.net/article/vue.js"></script> </head> <body> <div> <<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>

输出结果:

model表单控件绑定详解

3、v-model 绑定复选框

<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="https://www.jb51.net/article/vue.js"></script> </head> <body> <div> <input type="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

model表单控件绑定详解

<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="https://www.jb51.net/article/vue.js"></script> </head> <body> <div> <input type="checkbox" value="刘二狗" v-model="checkedNames"> <label for="jack">Jack</label> <input type="checkbox" value="张麻子" v-model="checkedNames"> <label for="john">John</label> <input type="checkbox" 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="https://www.jb51.net/article/vue.js"></script> </head> <body> <div> <input type="radio" value="One" v-model="picked"> <label for="one">One</label> <br> <input type="radio" 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="https://www.jb51.net/article/vue.js"></script> </head> <body> <div> <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>

输出结果:

多选列表

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wysdgg.html