将默认值设置为选项选择菜单 [英] Set default value to option select menu
问题描述
我想将自定义属性绑定到选项选择菜单. 标签将只具有
selected="selected"
<div><选择><option v-for="" v-bind:selected="[condition ? selected : notSelected]" value="">{{ resource.xprm_name }}</option></选择>
</模板>数据: {选定:选定",未选中的: ""}
这不起作用,但是如果我将 v-bind:selected
更改为 v-bind:class
然后它会收到适当的类,所以逻辑是有效的,但不是 selected
属性.
有什么方法可以让它与这样的自定义属性一起工作?
你可以使用 v-model
来选择一个选择框的默认值:
标记:
<select v-model="selected"><option value="foo">foo</option><option value="bar">Bar</option><option value="baz">Baz</option></选择>
查看模型:
new Vue({el: "#app",数据: {选择:'酒吧'}});
这是 JSFiddle:https://jsfiddle.net/Lxfxyqmf/
I want to bind a custom attribute to an option select menu.
The <option>
tag would simply have an attribute of selected="selected"
<template>
<div>
<select>
<option v-for="" v-bind:selected="[condition ? selected : notSelected]" value="">{{ resource.xprm_name }}</option>
</select>
</div>
</template>
data: {
selected: "selected",
notSelected: ""
}
This does not work, but if I change v-bind:selected
to v-bind:class
then it receives the appropriate class, so the logic is working, but not with the selected
attribute.
Any way to make it work with such custom attributes?
You can just use v-model
for selecting a default value on a select box:
Markup:
<div id="app">
<select v-model="selected">
<option value="foo">foo</option>
<option value="bar">Bar</option>
<option value="baz">Baz</option>
</select>
</div>
View Model:
new Vue({
el: "#app",
data: {
selected: 'bar'
}
});
Here's the JSFiddle: https://jsfiddle.net/Lxfxyqmf/
这篇关于将默认值设置为选项选择菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!