vue学习笔记9-表单数据双向绑定

案例1–表单数据绑定
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>表单数据绑定</title>
		<style type="text/css">
		</style>
		<script src="../js/vue-v2.6.12.js" type="text/javascript" charset="utf-8"></script>
	</head>
	<body>
		<div id="app">
			<input type="checkbox" v-model="checked" value="判断"/>{{checked}}<br>
			请选择你的爱好:<br>
			<input type="checkbox" v-model="checkedName" value="乒乓球"/>乒乓球<br>
			<input type="checkbox" v-model="checkedName" value="蓝球"/>蓝球<br>
			<input type="checkbox" v-model="checkedName" value="足球"/>足球<br>
			<input type="checkbox" v-model="checkedName" value="排球"/>排球<br>
			<input type="checkbox" v-model="checkedName" value="羽毛球"/>羽毛球<br>
			<input type="checkbox" v-model="checkedName" value="保龄球"/>保龄球<br>
			<input type="checkbox" v-model="checkedName" value="台球"/>台球<br>
			<input type="checkbox" v-model="checkedName" value="其他"/>其他<br>
			{{checkedName}}<br>
			请选择你的性别:<br>
			<input type="radio" name="sex" v-model="sex" value=""/><br>
			<input type="radio" name="sex" v-model="sex" value=""/><br>
			{{sex}}<br>
			请选择你的水果:<br>
			<select name="fruit" v-model="selected" multiple="multiple">
				<option value="苹果">苹果</option>
				<option value="香蕉">香蕉</option>
				<option value="橘子">橘子</option>
				<option value="葡萄">葡萄</option>
				<option value="芒果">芒果</option>
				<option value="柿子">柿子</option>
				<option value="李子">李子</option>
			</select><br>
			{{selected}}<br>
			<!-- 修饰符 -->
			<input type="text" v-model.lazy="text1" value="" />{{text1}}<br>
			<input type="number" name="" v-model.number="age"/>{{age}}<br>
			<input type="text" v-model.trim="text2" value="" />{{text2}}<br>
			<!-- 文本域 -->
			<textarea v-model="textarea"></textarea>
			<p :style="{whiteSpace:pre}">{{textarea}}</p>
		</div>
		<script type="text/javascript">
			var app = new Vue({
				el: "#app",
				data: {
					checked:false,
					checkedName:[],
					sex:"",
					selected:[],
					text1:'',
					age:0,
					text2:'',
					textarea:'',
					pre:"pre-line"
				}
			})
		</script>
	</body>
</html>
基础用法

它会根据控件类型自动选取正确的方法来更新元素。尽管有些神奇,但 v-model 本质上不过是语法糖。它负责监听用户的输入事件以更新数据,并对一些极端场景进行一些特殊处理。

v-model 会忽略所有表单元素的 value、checked、selected attribute 的初始值而总是将 Vue 实例的数据作为数据来源。你应该通过 JavaScript 在组件的 data 选项中声明初始值。

v-model 在内部为不同的输入元素使用不同的 property 并抛出不同的事件:

text 和 textarea 元素使用 value property 和 input 事件;
checkbox 和 radio 使用 checked property 和 change 事件;
select 字段将 value 作为 prop 并将 change 作为事件。

对于需要使用输入法 (如中文、日文、韩文等) 的语言,你会发现 v-model 不会在输入法组合文字过程中得到更新。如果你也想处理这个过程,请使用 input 事件。

文本
<input v-model="message" placeholder="edit me">
<p>Message is: {{ message }}</p>
多行文本

在文本区域插值 ({{text}}) 并不会生效,应用 v-model 来代替。

<span>Multiline message is:</span>
<p style="white-space: pre-line;">{{ message }}</p>
<!--vue的-会报错要将white-space写成whiteSpace
<p :style="{whiteSpace:pre}">{{textarea}}</p>-->
<br>
<textarea v-model="message" placeholder="add multiple lines"></textarea>
复选框

单个复选框,绑定到布尔值:

<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: []
  }
})
单选按钮
<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' }
    ]
  }
})
值绑定

对于单选按钮,复选框及选择框的选项,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 实例的一个动态 property 上,这时可以用 v-bind 实现,并且这个 property 的值可以不是字符串。

复选框
<input
  type="checkbox"
  v-model="toggle"
  true-value="yes"
  false-value="no"
>
// 当选中时
vm.toggle === 'yes'
// 当没有选中时
vm.toggle === 'no'

这里的 true-value 和 false-value attribute 并不会影响输入控件的 value attribute,因为浏览器在提交表单时并不会包含未被选中的复选框。如果要确保表单中这两个值中的一个能够被提交,(即“yes”或“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
修饰符
.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">
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值