12表单输入绑定 12.1基础用法 你可以用 v-model 指令在表单 <input>、<textarea> 及 <select> 元素上创建双向数据绑定。它会根据控件类型自动选取正确的方法来更新元素。 文本 <input v-model="message" placeholder="edit me"> <p>Message is: {{ message }}</p> 多行文本 <span>Multiline message is:</span> <p style="white-space: pre-line;">{{ message }}</p> <br> <textarea v-model="message" placeholder="add multiple lines"></textarea> 复选框 单个复选框,绑定到布尔值: <input type="checkbox" id="checkbox" v-model="checked"> <label for="checkbox">{{ checked }}</label> 多个复选框,绑定到同一个数组: <div id='example-3'> <input type="checkbox" id="jack" value="Jack" v-model="checkedNames"> <label for="jack">Jack</label> <input type="checkbox" id="john" value="John" v-model="checkedNames"> <label for="john">John</label> <input type="checkbox" id="mike" value="Mike" v-model="checkedNames"> <label for="mike">Mike</label> <br> <span>Checked names: {{ checkedNames }}</span> </div> new Vue({ el: '#example-3', data: { checkedNames: [] } }) 单选按钮 <div id="example-4"> <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> new Vue({ el: '#example-4', data: { picked: '' } }) 选择框 单选时: <div id="example-5"> <select v-model="selected"> <option disabled value="">请选择</option> <option>A</option> <option>B</option> <option>C</option> </select> <span>Selected: {{ selected }}</span> </div> new Vue({ el: '...', data: { selected: '' } }) 多选时 (绑定到一个数组): <div id="example-6"> <select v-model="selected" multiple style="width: 50px;"> <option>A</option> <option>B</option> <option>C</option> </select> <br> <span>Selected: {{ selected }}</span> </div> new Vue({ el: '#example-6', data: { selected: [] } }) 用 v-for 渲染的动态选项: <select v-model="selected"> <option v-for="option in options" v-bind:value="option.value"> {{ option.text }} </option> </select> <span>Selected: {{ selected }}</span> new Vue({ el: '...', data: { selected: 'A', options: [ { text: 'One', value: 'A' }, { text: 'Two', value: 'B' }, { text: 'Three', value: 'C' } ] } }) 12.2值绑定 对于单选按钮,复选框及选择框的选项,v-model 绑定的值通常是静态字符串 (对于复选框也可以是布尔值): <!-- 当选中时,`picked` 为字符串 "a" --> <input type="radio" v-model="picked" value="a"> <!-- `toggle` 为 true 或 false --> <input type="checkbox" v-model="toggle"> <!-- 当选中第一个选项时,`selected` 为字符串 "abc" --> <select v-model="selected"> <option value="abc">ABC</option> </select> 但是有时我们可能想把值绑定到 Vue 实例的一个动态属性上,这时可以用 v-bind 实现,并且这个属性的值可以不是字符串。 复选框 <input type="checkbox" v-model="toggle" true-value="yes" false-value="no" > // 当选中时 vm.toggle === 'yes' // 当没有选中时 vm.toggle === 'no' 单选按钮 <input type="radio" v-model="pick" v-bind:value="a"> // 当选中时 vm.pick === vm.a 选择框的选项 <select v-model="selected"> <!-- 内联对象字面量 --> <option v-bind:value="{ number: 123 }">123</option> </select> // 当选中时typeof vm.selected // => 'object' vm.selected.number // => 123 12.3修饰符 .lazy 在默认情况下,v-model 在每次 input 事件触发后将输入框的值与数据进行同步 (除了上述输入法组合文字时)。你可以添加 lazy 修饰符,从而转变为使用 change 事件进行同步: <!-- 在“change”时而非“input”时更新 --> <input v-model.lazy="msg" > .number 如果想自动将用户的输入值转为数值类型,可以给 v-model 添加 number 修饰符: <input v-model.number="age" type="number"> 这通常很有用,因为即使在 type="number" 时,HTML 输入元素的值也总会返回字符串。如果这个值无法被 parseFloat() 解析,则会返回原始的值。 .trim 如果要自动过滤用户输入的首尾空白字符,可以给 v-model 添加 trim 修饰符: <input v-model.trim="msg"> 完整代码: 12 表单输入绑定1.html <!DOCTYPE html> <html> <head> <title>表单输入绑定</title> <script src=" https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> </head> <body> <ul id="example-1"> <input v-model="message" placeholder="edit me"> <p>Message is: {{ message }}</p> </ul> <script> var example1 = new Vue({ el: '#example-1', data: { message: '' } }) </script> </body> </html> 12 表单输入绑定2.html <!DOCTYPE html> <html> <head> <title>表单输入绑定</title> <script src=" https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> </head> <body> <ul id="example-2"> <span>Multiline message is:</span> <p style="white-space: pre-line;">{{ message }}</p> <br> <textarea v-model="message" placeholder="add multiple lines"></textarea> </ul> <script> var example1 = new Vue({ el: '#example-2', data: { message: '' } }) </script> </body> </html> 12 表单输入绑定3.html <!DOCTYPE html> <html> <head> <title>表单输入绑定</title> <script src=" https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> </head> <body> <ul id="example-3"> <input type="checkbox" id="checkbox" v-model="checked"> <label for="checkbox">{{ checked }}</label> </ul> <script> var example1 = new Vue({ el: '#example-3', data: { checked: true } }) </script> </body> </html> 12 表单输入绑定4.html <!DOCTYPE html> <html> <head> <title>表单输入绑定</title> <script src=" https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> </head> <body> <div id='example-3'> <input type="checkbox" id="jack" value="Jack" v-model="checkedNames"> <label for="jack">Jack</label> <input type="checkbox" id="john" value="John" v-model="checkedNames"> <label for="john">John</label> <input type="checkbox" id="mike" value="Mike" v-model="checkedNames"> <label for="mike">Mike</label> <br> <span>Checked names: {{ checkedNames }}</span> </div> <script> new Vue({ el: '#example-3', data: { checkedNames: [] } }) </script> </body> </html> 12 表单输入绑定5.html <!DOCTYPE html> <html> <head> <title>表单输入绑定</title> <script src=" https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> </head> <body> <div id="example-4"> <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> <script> new Vue({ el: '#example-4', data: { picked: '' } }) </script> </body> </html> 12 表单输入绑定6.html <!DOCTYPE html> <html> <head> <title>表单输入绑定</title> <script src=" https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> </head> <body> <div id="example-5"> <select v-model="selected"> <option disabled value="">请选择</option> <option>A</option> <option>B</option> <option>C</option> </select> <span>Selected: {{ selected }}</span> </div> <script> new Vue({ el: '#example-5', data: { selected: '' } }) </script> </body> </html> 12 表单输入绑定7.html <!DOCTYPE html> <html> <head> <title>表单输入绑定</title> <script src=" https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> </head> <body> <div id="example-6"> <select v-model="selected" multiple style="width: 50px;"> <option>A</option> <option>B</option> <option>C</option> </select> <br> <span>Selected: {{ selected }}</span> </div> <script> new Vue({ el: '#example-6', data: { selected: [] } }) </script> </body> </html> 12 表单输入绑定8.html <!DOCTYPE html> <html> <head> <title>表单输入绑定</title> <script src=" https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> </head> <body> <div id="example-6"> <select v-model="selected"> <option v-for="option in options" v-bind:value="option.value"> {{ option.text }} </option> </select> <span>Selected: {{ selected }}</span> </div> <script> new Vue({ el: '#example-6', data: { selected: 'A', options: [ { text: 'One', value: 'A' }, { text: 'Two', value: 'B' }, { text: 'Three', value: 'C' } ] } }) </script> </body> </html> 12 表单输入绑定9.html <!DOCTYPE html> <html> <head> <title>表单输入绑定</title> <script src=" https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> </head> <body> <div id="example-6"> <!-- 当选中时,`picked` 为字符串 "a" --> <input type="radio" v-model="picked" value="a"><br> {{picked}} <!-- `toggle` 为 true 或 false --> <input type="checkbox" v-model="toggle"><br> {{toggle}} <!-- 当选中第一个选项时,`selected` 为字符串 "abc" --> <select v-model="selected"> <option value="a">aaa</option> <option value="b">bbb</option> <option value="c">ccc</option> </select><br> {{selected}} <input type="checkbox" v-model="toggle" true-value="yes" false-value="no" > <input type="radio" v-model="pick" v-bind:value="a" v-on:click="onClick"> <select v-model="selected"> <!-- 内联对象字面量 --> <option v-bind:value="{ number: 123 }">123</option> </select> </div> <script> new Vue({ el: '#example-6', data: { picked: '', toggle: true, selected: '', pick: false }, methods: { onClick: function () { alert(this.pick === this.a) } } }) </script> </body> </html> 12 表单输入绑定10.html <!DOCTYPE html> <html> <head> <title>表单输入绑定</title> <script src=" https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> </head> <body> <div id="example-6"> <!-- 在“change”时而非“input”时更新 --> <input v-model.lazy="msg" > {{msg}} <br> <input v-model.number="age" type="number"> {{age}} <br> <input v-model.trim="msg2"> {{msg2}} </div> <script> new Vue({ el: '#example-6', data: { msg: '', age: 0, msg2: '' }, methods: { } }) </script> </body> </html>
欢迎观看视频教程:https://ke.qq.com/course/432961?tuin=36914f34,如有疑问,请加QQ群665714453交流讨论。
转载于:https://www.cnblogs.com/daqiang123/p/11368419.html