Vue的表单控件绑定
来源:互联网 发布:autodesk 360 mac卸载 编辑:程序博客网 时间:2024/04/28 13:43
基础用法
你可以用 v-model
指令在表单控件元素上创建双向数据绑定。它会根据控件类型自动选取正确的方法来更新元素。尽管有些神奇,但 v-model
本质上不过是语法糖,它负责监听用户的输入事件以更新数据,并特别处理一些极端的例子。
v-model
will ignore the initial value
, checked
or selected
attributes found on any form elements. It will always treat the Vue instance data as the source of truth. You should declare the initial value on the JavaScript side, inside the data
option of your component.
对于需要输入法编辑器的语言(中文、日文、韩文等),要注意的是,在 IME 字母组合窗口输入时 v-model
并不会更新。如果你想在此期间满足更新需求,请使用 input
事件。
文本
<input v-model="message" placeholder="edit me"><p>Message is: {{ message }}</p>
Message is:
多行文本
<span>Multiline message is:</span><p style="white-space: pre">{{ message }}</p><br><textarea v-model="message" placeholder="add multiple lines"></textarea>
在文本区域插值( <textarea></textarea>
) 并不会生效,应用 v-model
来代替
复选框
单个勾选框,逻辑值:
<input type="checkbox" id="checkbox" v-model="checked"><label for="checkbox">{{ checked }}</label>
多个勾选框,绑定到同一个数组:
<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>
new Vue({el: '...',data: {checkedNames: []}})
Checked names: []
单选按钮
<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>
Picked:
选择列表
单选列表:
<select v-model="selected"><option disabled value="">Please select one</option><option>A</option><option>B</option><option>C</option></select><span>Selected: {{ selected }}</span>
new Vue({el: '...',data: {selected: ''}})
If the initial value of your v-model
expression does not match any of the options, the <select>
element will render in an “unselected” state. On iOS this will cause the user not being able to select the first item because iOS does not fire a change event in this case. It is therefore recommended to provide a disabled option with an empty value, as demonstrated in the example above.
多选列表(绑定到一个数组):
<select v-model="selected" multiple><option>A</option><option>B</option><option>C</option></select><br><span>Selected: {{ selected }}</span>
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' }]}})
绑定 value
对于单选按钮,勾选框及选择列表选项, v-model
绑定的 value 通常是静态字符串(对于勾选框是逻辑值):
<!-- 当选中时,`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>
但是有时我们想绑定 value 到 Vue 实例的一个动态属性上,这时可以用 v-bind
实现,并且这个属性的值可以不是字符串。
复选框
<inputtype="checkbox"v-model="toggle"v-bind:true-value="a"v-bind:false-value="b">
// 当选中时vm.toggle === vm.a// 当没有选中时vm.toggle === vm.b
单选按钮
<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
修饰符
.lazy
在默认情况下, v-model
在 input
事件中同步输入框的值与数据 (除了 上述 IME 部分),但你可以添加一个修饰符 lazy
,从而转变为在 change
事件中同步:
<!-- 在 "change" 而不是 "input" 事件中更新 --><input v-model.lazy="msg" >
.number
如果想自动将用户的输入值转为 Number 类型(如果原值的转换结果为 NaN 则返回原值),可以添加一个修饰符 number
给 v-model
来处理输入值:
<input v-model.number="age" type="number">
这通常很有用,因为在 type="number"
时 HTML 中输入的值也总是会返回字符串类型。
.trim
如果要自动过滤用户输入的首尾空格,可以添加 trim
修饰符到 v-model
上过滤输入:
<input v-model.trim="msg">
在组件中使用 v-model
如果你还不熟悉 Vue 组件,现在先跳过此处。
HTML 内置的输入框类型并不能满足需求。幸运的是,Vue 组件允许使用完全自定义的行为来构建可重用的输入框。这些输入框甚至可以使用 v-model
!想要了解更多信息,请阅读组件指南中自定义输入。
原文:http://vuejs.org/guide/forms.html
- Vue的表单控件绑定
- vue表单控件绑定
- Vue-表单控件绑定
- Vue表单控件绑定的思考
- 表单控件绑定vue.js
- vue.js 表单控件 输入绑定 v-model的使用
- Vue.js笔记-表单控件绑定
- 06、vue.js 之表单控件绑定
- Vue基础之表单控件绑定
- Vue.js 学习8 表单控件绑定
- vue.js表单的双向绑定
- Vue 组件实现表单的双向绑定
- Vue.js学习 Item9 – 表单控件绑定
- Vue.js 学习(8) -- v-model: 表单控件绑定
- vue 表单 绑定
- vue表单事件绑定
- VUE表单输入绑定
- Vue表单及表单绑定
- 二叉树总结
- Struts2 Session中赋值与页面取值
- 密码 验证码登陆 同一页面
- 活动选择问题 (sdut oj)
- 常用语句
- Vue的表单控件绑定
- 每个程序猿必读的10篇文章
- unmappable character for encoding UTF-8
- Spirng 基于session国际化
- PHP的fsockopen函数详解
- LBP等价模式降维处理
- 网上图书商城视频教程day03
- zeppelin权限问题
- yum安装MySQL,及配置用户和远程登录