vue input v-model
v-model 主要用于input、textarea、select等表单控件元素上创建双向数据绑定,所谓双向绑定就是js中vue实例化的data对象的数据与其渲染的dom元素上的内容保持一致1、text 文本1234div id="app"> h1>{{message}}h1>
·
1
2
3
4
|
<
div
id
=
"app"
>
<
h1
>{{message}}</
h1
>
<
input
type
=
"text"
v-model
=
"message"
/>
</
div
>
|
1
2
3
4
5
6
|
new
Vue({
el:
'#app'
,
data:{
message:
'Hello World !'
}
})
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<!--多行文本-->
<
div
id
=
"text"
>
<
p
>Multiline message is: {{msg}}</
p
>
<
textarea
v-model
=
"msg"
></
textarea
>
</
div
>
<
script
src
=
"https://unpkg.com/vue/dist/vue.js"
></
script
>
<
script
>
var vm = new Vue({
el:'#text',
data:{
msg:''
}
})
</
script
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<!--单个勾选框,逻辑值-->
<
div
id
=
"app"
>
<
input
type
=
"checkbox"
id
=
"checkbox"
v-model
=
"checked"
>
<
label
for
=
"checkbox"
>{{checked}}</
label
>
</
div
>
<
script
src
=
"https://unpkg.com/vue/dist/vue.js"
></
script
>
<
script
>
var vm1 = new Vue({
el:'#app',
data:{
checked:true
}
});
</
script
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<!--多个勾选框,绑定到同一个数组-->
<div id=
"app1"
>
<input type=
"checkbox"
id=
"jack"
value=
"jack"
v-model=
"checkNames"
>
<label
for
=
"jack"
>Jack</label>
<input type=
"checkbox"
id=
"jhon"
value=
"jhon"
v-model=
"checkNames"
>
<label
for
=
"jhon"
>Jhon</label>
<input type=
"checkbox"
id=
"mike"
value=
"mike"
v-model=
"checkNames"
>
<label
for
=
"mike"
>Mike</label>
<p>Checked Names: {{checkNames}}</p>
</div>
<script src=
"https://unpkg.com/vue/dist/vue.js"
></script>
<script>
var
vm1 =
new
Vue({
el:
'#app'
,
data:{
checked:
true
}
});
</script>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<!--单选按钮-->
<
div
id
=
"app2"
>
<
input
type
=
"radio"
id
=
"male"
value
=
"male"
v-model
=
"gender"
>
<
label
for
=
"male"
>Male</
label
>
<
input
type
=
"radio"
id
=
"female"
value
=
"female"
v-model
=
"gender"
>
<
label
for
=
"female"
>Female</
label
>
<
p
>Gender: {{gender}}</
p
>
</
div
>
<
script
src
=
"https://unpkg.com/vue/dist/vue.js"
></
script
>
<
script
>
var vm3 = new Vue({
el:'#app2',
data:{
gender:''
}
});
</
script
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<!--单选列表-->
<
div
id
=
"app3"
>
<
select
v-model
=
"selected"
>
<
option
>A</
option
>
<
option
>B</
option
>
<
option
>C</
option
>
</
select
>
<
p
>Selected: {{selected}}</
p
>
</
div
>
<
script
src
=
"https://unpkg.com/vue/dist/vue.js"
></
script
>
<
script
>
var vm4 = new Vue({
el:'#app3',
data:{
selected:''
}
});
</
script
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<!--多选列表-->
<
div
id
=
"app4"
>
<
select
v-model
=
"selected"
multiple>
<
option
>A</
option
>
<
option
>B</
option
>
<
option
>C</
option
>
</
select
>
<
p
>Selected: {{selected}}</
p
>
</
div
>
<
script
src
=
"https://unpkg.com/vue/dist/vue.js"
></
script
>
<
script
>
var vm5 = new Vue({
el:'#app4',
data:{
selected:[]
}
});
</
script
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<!--动态选项,用v-for渲染-->
<
div
id
=
"app5"
>
<
select
v-model
=
"selected"
>
<
option
v-for
=
"option in options"
v-bind:value
=
"option.value"
>{{option.text}}</
option
>
</
select
>
<
p
>Selected: {{selected}}</
p
>
</
div
>
<
script
src
=
"https://unpkg.com/vue/dist/vue.js"
></
script
>
<
script
>
var vm6 = new Vue({
el:'#app5',
data:{
selected:'A',
options:[
{text:'One', value:'A'},
{text:'Two', value:'B'},
{text:'Three', value:'C'}
]
}
});
</
script
>
|
1
2
3
4
5
6
7
8
9
10
|
<!-- 当选中时,`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
>
|
1
2
3
4
5
6
7
8
9
|
<!--复选框-->
<
div
id
=
"app1"
>
<
input
type
=
"checkbox"
v-model
=
"toggle"
v-bind:true-value
=
"a"
v-bind:false-value
=
"b"
>
</
div
>
// 当选中时
vm.toggle === vm.a
// 当没有选中时
vm.toggle === vm.b
|
1
2
3
|
<
input
type
=
"radio"
v-model
=
"pick"
v-bind:value
=
"a"
>
// 当选中时vm.pick === vm.a
|
1
2
3
4
5
6
7
8
|
<
select
v-model
=
"selected"
>
<!-- 内联对象字面量 -->
<
option
v-bind:value
=
"{ number: 123 }"
>123</
option
>
</
select
>
// 当选中时
typeof vm.selected // -> 'object'
vm.selected.number // -> 123
|
1
2
3
4
5
6
7
|
<
div
id
=
"app"
>
<
h1
>{{message}}</
h1
>
<
input
type
=
"text"
v-model
=
"message"
/>
<
pre
>
{{$data|json}}
</
pre
>
</
div
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<div id=
"app"
>
<input type=
"text"
v-model=
"msg"
number><br>
{{msg}}
</div>
<script src=
"js/vue.js"
></script>
<script>
new
Vue({
el:
'#app'
,
data:{
msg:
'仅处理number类型的数字'
}
})
</script>
<!-- 另外一种写法
<div id=
"app1"
>
<input v-model.number=
"age"
type=
"text"
>
<p>My age is: {{age}}</p>
</div>
-->
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<div id=
"app"
>
<input type=
"text"
v-model=
"msg"
lazy><br>
{{msg}}
</div>
<script src=
"js/vue.js"
></script>
<script>
new
Vue({
el:
'#app'
,
data:{
msg:
'内容是在change事件后才改变的'
}
})
</script>
|
1
2
3
4
5
6
7
8
9
10
11
12
|
<div id=
"app"
>
<input type=
"text"
v-model=
"msg"
debounce=
"5000"
><br>
{{msg}}
</div>
<script src=
"js/vue.js"
></script>
<script>
new
Vue({
el:
'#app'
,
data:{
msg:
'内容是在5s后才改变的'
}
})
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<
div
id
=
"app1"
>
<
input
v-model
=
"msg1"
type
=
"text"
>
<
p
>The common message is: {{msg1}}</
p
>
<
hr
>
<
input
v-model.trim
=
"msg2"
type
=
"text"
>
<
p
>The trim message is: {{msg2}}</
p
>
</
div
>
<
script
src
=
"https://unpkg.com/vue/dist/vue.js"
></
script
>
<
script
>
var vm1 = new Vue({
el:'#app1',
data:{
msg1:'',
msg2:''
}
})
|
更多推荐
已为社区贡献3条内容
所有评论(0)