• vue--v-model表单控件绑定


     

    原文链接:https://www.cnblogs.com/dyfbk/p/6868350.html

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

    1、v-model 双向绑定文本

    复制代码
     1 <!DOCTYPE html>
     2 <html>
     3 <head lang="en">
     4     <meta charset="UTF-8">
     5     <title></title>
     6     <script type="text/javascript"  src="vue.js"></script>
     7 </head>
     8 <body>
     9 <div id="app">
    10     <input v-model="message" placeholder="edit me">
    11     <p>Message is: {{ message }}</p>
    12 </div>
    13 </body>
    14 <script>
    15     var vm = new Vue({
    16         el:"#app",
    17         data: {
    18             message: '绑定文本'
    19         }
    20     })
    21 </script>
    22 </html>
    复制代码

    输出结果:

    2、v-model 双向绑定多行文本,与上面的例子相似。

    复制代码
     1 <!DOCTYPE html>
     2 <html>
     3 <head lang="en">
     4     <meta charset="UTF-8">
     5     <title></title>
     6     <script type="text/javascript"  src="vue.js"></script>
     7 </head>
     8 <body>
     9 <div id="app">
    10     <<span>Multiline message is:</span>
    11     <p style="white-space: pre">{{ message }}</p>
    12     <br>
    13     <textarea v-model="message" placeholder="add multiple lines"></textarea>
    14 </div>
    15 </body>
    16 <script>
    17     var vm = new Vue({
    18         el:"#app",
    19         data: {
    20             message: '绑定多行文本'
    21         }
    22     })
    23 </script>
    24 </html>
    复制代码

    输出结果:

    3、v-model 绑定复选框

    复制代码
     1 <!DOCTYPE html>
     2 <html>
     3 <head lang="en">
     4     <meta charset="UTF-8">
     5     <title></title>
     6     <script type="text/javascript"  src="vue.js"></script>
     7 </head>
     8 <body>
     9 <div id="app">
    10     <input type="checkbox" id="checkbox" v-model="checked">
    11     <label for="checkbox">{{ checked }}</label>
    12 </div>
    13 </body>
    14 <script>
    15     var vm = new Vue({
    16         el:"#app",
    17         data: {
    18             checked: 'true'
    19         }
    20     })
    21 </script>
    22 </html>
    复制代码

    输出结果:选中为true   不选中则为false

    复制代码
     1 <!DOCTYPE html>
     2 <html>
     3 <head lang="en">
     4     <meta charset="UTF-8">
     5     <title></title>
     6     <script type="text/javascript"  src="vue.js"></script>
     7 </head>
     8 <body>
     9 <div id="app">
    10     <input type="checkbox" id="jack" value="刘二狗" v-model="checkedNames">
    11     <label for="jack">Jack</label>
    12     <input type="checkbox" id="john" value="张麻子" v-model="checkedNames">
    13     <label for="john">John</label>
    14     <input type="checkbox" id="mike" value="小狗子" v-model="checkedNames">
    15     <label for="mike">Mike</label>
    16     <br>
    17     <span>Checked names: {{ checkedNames }}</span>
    18 </div>
    19 </body>
    20 <script>
    21     var vm = new Vue({
    22         el:"#app",
    23         data: {
    24             checkedNames: []
    25         }
    26     })
    27 </script>
    28 </html>
    复制代码

    输出结果:

    4、v-model 绑定单选按钮

    复制代码
     1 <!DOCTYPE html>
     2 <html>
     3 <head lang="en">
     4     <meta charset="UTF-8">
     5     <title></title>
     6     <script type="text/javascript"  src="vue.js"></script>
     7 </head>
     8 <body>
     9 <div id="app">
    10     <input type="radio" id="one" value="One" v-model="picked">
    11     <label for="one">One</label>
    12     <br>
    13     <input type="radio" id="two" value="Two" v-model="picked">
    14     <label for="two">Two</label>
    15     <br>
    16     <span>Picked: {{ picked }}</span>
    17 </div>
    18 </body>
    19 <script>
    20     var vm = new Vue({
    21         el:"#app",
    22         data: {
    23             picked: ''
    24         }
    25     })
    26 </script>
    27 </html>
    复制代码

    输出结果:

    5、v-model 绑定下拉列表

    复制代码
     1 <!DOCTYPE html>
     2 <html>
     3 <head lang="en">
     4     <meta charset="UTF-8">
     5     <title></title>
     6     <script type="text/javascript"  src="vue.js"></script>
     7 </head>
     8 <body>
     9 <div id="app">
    10     <select v-model="selected">
    11         <option>A</option>
    12         <option>B</option>
    13         <option>C</option>
    14     </select>
    15     <span>Selected: {{ selected }}</span>
    16 </div>
    17 </body>
    18 <script>
    19     var vm = new Vue({
    20         el:"#app",
    21         data: {
    22             selected: ''
    23         }
    24     })
    25 </script>
    26 </html>
    复制代码

    输出结果:

    多选列表

    复制代码
     1 <!DOCTYPE html>
     2 <html>
     3 <head lang="en">
     4     <meta charset="UTF-8">
     5     <title></title>
     6     <script type="text/javascript"  src="vue.js"></script>
     7 </head>
     8 <body>
     9 <div id="app">
    10     <select v-model="selected" multiple style=" 50px">
    11         <option>A</option>
    12         <option>B</option>
    13         <option>C</option>
    14     </select>
    15     <br>
    16     <span>Selected: {{ selected }}</span>
    17 </div>
    18 </body>
    19 <script>
    20     var vm = new Vue({
    21         el:"#app",
    22         data: {
    23             selected: []
    24         }
    25     })
    26 </script>
    27 </html>
    复制代码

    输出结果:

    6、动态选项,用 v-for 渲染:

    复制代码
     1 <!DOCTYPE html>
     2 <html>
     3 <head lang="en">
     4     <meta charset="UTF-8">
     5     <title></title>
     6     <script type="text/javascript"  src="vue.js"></script>
     7 </head>
     8 <body>
     9 <div id="app">
    10     <select v-model="selected">
    11         <option v-for="option in options" v-bind:value="option.value">
    12             {{ option.text }}
    13         </option>
    14     </select>
    15     <span>Selected: {{ selected }}</span>
    16 </div>
    17 </body>
    18 <script>
    19     var vm = new Vue({
    20         el:"#app",
    21         data: {
    22             selected: 'A',
    23             options: [
    24                 { text: 'One', value: 'A' },
    25                 { text: 'Two', value: 'B' },
    26                 { text: 'Three', value: 'C' }
    27             ]
    28         }
    29     })
    30 </script>
    31 </html>
    复制代码

    输出结果:

  • 相关阅读:
    NYoj-119-士兵杀敌(3)-RMQ算法
    springMVC3学习(九)--redirect和forward跳转
    STL
    在 Ubuntu 12.04 上通过源码安装 Open vSwitch (OVS)
    SSO 基于CAS实现单点登录 实例解析(二)
    Linux
    linux的子进程调用exec( )系列函数
    以Settings.APPLICATION_DEVELOPMENT_SETTINGS打开开发人员面板出错总结
    python学习记录
    CentOS6.X下安装配置独立SVN服务器Subversion server
  • 原文地址:https://www.cnblogs.com/vinieo/p/9915894.html
Copyright © 2020-2023  润新知