【问题标题】:Set the value for <select> without using 'v-model'设置 <select> 的值而不使用 'v-model'
【发布时间】:2017-07-31 02:34:39
【问题描述】:

我想要类似下面的 Vue 模板,但它不适用于属性 selectedVal。正确的属性名称是什么?

<select :selectedVal="myList" multiple>
  <option value="abc">ABC</option>
</select>

对于&lt;input&gt;,要绑定的道具是value,但对于&lt;select&gt;,它似乎没有在任何地方记录。我正在寻找要绑定的正确道具的名称。

【问题讨论】:

  • 你想通过v-for生成选项标签,还是固定?
  • 顺便说一句,我认为selected 是选项标签中的属性,而不是选择标签中的属性。
  • @choasia:我的选项是使用 v-for 生成的。
  • 是你需要的吗:jsfiddle.net/choasia/pncrudf8 ?
  • 它可能有效,但这不是我想知道的。

标签: vue.js vuejs2


【解决方案1】:

我回答是因为很难在评论中引用代码 sn-p。

我想您是在查看文档 here 后提出这个问题的,该文档展示了一种定义选定项目的优雅方式。

<select v-model="selected">
  <option v-for="option in options" v-bind:value="option.value">
    {{ option.text }}
  </option>
</select>

实际上,我怀疑是否存在这样的 v-bind 道具,因为 value is an attribute of input and option,但 selected is not an attribute of select but an attribute of option 和我在 select 中找不到任何替代品。

另外,在浏览了 Vue.js 的源代码之后,我没有看到 vue 对绑定属性(代码 herehere)而不是将 v-bind 的值推入元素的 @987654337 @ 列表。

export function addAttr (el: ASTElement, name: string, value: string) {
  (el.attrs || (el.attrs = [])).push({ name, value })
}

另一方面,select 似乎是 v-model 中的一个特例(参见代码 here)。
因此,我通过将selected 绑定到选项而不是select,在评论中提供了the solution

【讨论】:

  • 非常感谢您的帮助。
  • @Vincent 一点也不。
猜你喜欢
  • 1970-01-01
  • 2020-05-01
  • 2019-11-13
  • 2020-05-26
  • 2018-12-14
  • 1970-01-01
  • 2019-05-22
  • 2021-11-19
  • 2018-12-25
相关资源
最近更新 更多