简体   繁体   English

默认 Select 选项是多个 Select 没有被明显选中。 (VueJs3)

[英]Default Select Option is Multiple Select is not visibly selected. (VueJs3)

I have created a Select Option, in which one option is by default selected.我创建了一个 Select 选项,其中一个选项默认选中。 In example one below, the default selected option is visibly selected within the select bar before opening it.在下面的示例中,默认选择的选项在打开之前在 select 栏中明显选择。

<select v-model="formData.replyMethod">
 <option value="" selected {{ $t(`files.homeName`)}}</option> # This is visibly selected
 <option value="1" selected {{ $t(`files.foo`)}}</option>
 <option value="2" selected {{ $t(`files.bar`)}}</option>
</select>

However when moving to a Computed Property & V-For, the selected option is selected, but not visibly (you have to open the list to see that now), in its place is a blank bar.但是,当移动到 Computed Property & V-For 时,选择的选项被选中,但不可见(您现在必须打开列表才能看到),取而代之的是一个空白栏。

What is causing the difference in output between these two ways of creating a list?是什么导致这两种创建列表的方式在 output 中的差异?

Example 2:示例 2:

<select v-model="formData.replyMethod">
        <option v-bind:value="selectedOption.id" selected>{{ $t(selectedOption.name) }}</option>
        <option v-bind:value="selectOpts.id" v-for="selectOpt in selectOpts">{{ $t(selectOpt.name) }}</option>
        </select>

const selectedOption = computed(() => {
  if(fooBarVariable) {
    let opt =  {id: 1, name: 'Foo'};
    return opt
  }
});

const selectOpts = [{ id: null, name: 'files.placeholder'},{id: 1, name: 'files.reply'},{id: 2, name: 'files.example'}]

When using v-model with select input, don't use selected attribute to set a default value for your select input.当使用带有 select 输入的 v-model 时,不要使用 selected 属性为您的 select 输入设置默认值。

If you're setting the value for each option by selectedOption.id then simply type the id of the option you would like it to be visible in formData.replyMethod .如果您通过selectedOption.id为每个选项设置值,则只需键入您希望它在formData.replyMethod中可见的选项的 id。

For example:例如:

 const selectOpts = [{ id: 1, name: 'files.placeholder'}]; const formData = { replyMethod: 1, };
 <select v-model="formData.replyMethod"> <option v-for="option in selectOpts":value="option.id"> {{ option.text }} </option> </select>

This will set an initial value for your input on component mount which will be overridden upon the user action (Same behavior as selected attribute)这将为您在组件安装上的输入设置一个初始值,该值将在用户操作时被覆盖(与selected属性的行为相同)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM