如何在不使用v-model的情况下通过代码更改vuetify中的v-select选项。

huangapple go评论57阅读模式
英文:

How to change v-select option in vuetify by code without v-model

问题

Sure, here's the translated code portion:

我需要通过代码设置 v-select 中的选项

我无法使用 v-model

我在 https://codepen.io/luizalves/pen/OJBYbQZ?editors=101 创建了一个 CodePen。

我需要

1.  v-select 中选择一个选项
2. 点击 "Set Select" 按钮更改选项

我的问题是按钮点击仅在第一次起作用

我做错了什么

// 标记

<div id="app">
  <v-app id="inspire">
    <v-container fluid>
      <v-row align="center">
        <v-col cols="6">
          <v-subheader>
            自定义项目
          </v-subheader>
        </v-col>
        <v-btn @click="setSelect()">设置选择</v-btn>
        <v-col cols="6">
          <v-select
            :value="select"
            :items="items"
            item-text="state"
            item-value="abbr"
            label="选择"
            persistent-hint
            single-line
          ></v-select>
        </v-col>
      </v-row>
    </v-container>
  </v-app>
</div>

// 脚本

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data () {
    return {
      select: 'FL' ,
      items: [
        { state: '佛罗里达', abbr: 'FL' },
        { state: '乔治亚', abbr: 'GA' },
        { state: '内布拉斯加', abbr: 'NE' },
        { state: '加利福尼亚', abbr: 'CA' },
        { state: '纽约', abbr: 'NY' },
      ],
    }
  },
  methods:{
    setSelect(){
      this.select="GA"
    },
    
  }
})

Is there anything else you need?

英文:

I need to set a option in v-select by code.

I can´t use v-model.

I created a codepen in https://codepen.io/luizalves/pen/OJBYbQZ?editors=101

I need:

  1. Choice a select option in v-select
  2. Change the option clicking the button "Set Select"

My problem is that the button click is working only on the first time.

What am I doing wrong?

//markup

&lt;div id=&quot;app&quot;&gt;
&lt;v-app id=&quot;inspire&quot;&gt;
&lt;v-container fluid&gt;
&lt;v-row align=&quot;center&quot;&gt;
&lt;v-col cols=&quot;6&quot;&gt;
&lt;v-subheader&gt;
Custom items
&lt;/v-subheader&gt;
&lt;/v-col&gt;
&lt;v-btn @click=&quot;setSelect()&quot;&gt;Set Select&lt;/v-btn&gt;
&lt;v-col cols=&quot;6&quot;&gt;
&lt;v-select
:value=&quot;select&quot;
:items=&quot;items&quot;
item-text=&quot;state&quot;
item-value=&quot;abbr&quot;
label=&quot;Select&quot;
persistent-hint
single-line
&gt;&lt;/v-select&gt;
&lt;/v-col&gt;
&lt;/v-row&gt;
&lt;/v-container&gt;
&lt;/v-app&gt;
&lt;/div&gt;

//script

new Vue({
el: &#39;#app&#39;,
vuetify: new Vuetify(),
data () {
return {
select: &#39;FL&#39; ,
items: [
{ state: &#39;Florida&#39;, abbr: &#39;FL&#39; },
{ state: &#39;Georgia&#39;, abbr: &#39;GA&#39; },
{ state: &#39;Nebraska&#39;, abbr: &#39;NE&#39; },
{ state: &#39;California&#39;, abbr: &#39;CA&#39; },
{ state: &#39;New York&#39;, abbr: &#39;NY&#39; },
],
}
},
methods:{
setSelect(){
this.select=&quot;GA&quot;
},
}
})

答案1

得分: 1

你可以使用@change事件来设置选定的值:

<v-select
  :value="select"
  :items="items"
  item-text="state"
  item-value="abbr"
  label="Select"
  persistent-hint
  single-line
  @change="setSelected"
></v-select>

在这段代码中,@change事件用于设置选定的值,其中:value绑定到select:items绑定到itemsitem-text指定显示文本,item-value指定值,label为选择框提供标签,persistent-hintsingle-line用于调整外观。

英文:

You can use @change event and set selected value:

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-js -->

new Vue({
el: &#39;#app&#39;,
vuetify: new Vuetify(),
data () {
return {
select: &#39;FL&#39; ,
items: [
{ state: &#39;Florida&#39;, abbr: &#39;FL&#39; },
{ state: &#39;Georgia&#39;, abbr: &#39;GA&#39; },
{ state: &#39;Nebraska&#39;, abbr: &#39;NE&#39; },
{ state: &#39;California&#39;, abbr: &#39;CA&#39; },
{ state: &#39;New York&#39;, abbr: &#39;NY&#39; },
],
}
},
methods:{
setSelect(){
this.select = &quot;GA&quot;
},
setSelected(e){
this.select = e
},
}
})

<!-- language: lang-html -->

&lt;link href=&quot;https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900&quot; rel=&quot;stylesheet&quot;&gt;
&lt;link href=&quot;https://cdn.jsdelivr.net/npm/@mdi/font@6.x/css/materialdesignicons.min.css&quot; rel=&quot;stylesheet&quot;&gt;
&lt;link href=&quot;https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.css&quot; rel=&quot;stylesheet&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui&quot;&gt;
&lt;div id=&quot;app&quot;&gt;
&lt;v-app id=&quot;inspire&quot;&gt;
&lt;v-container fluid&gt;
&lt;v-row align=&quot;center&quot;&gt;
&lt;v-col cols=&quot;6&quot;&gt;
&lt;v-subheader&gt;
Custom items
&lt;/v-subheader&gt;
&lt;/v-col&gt;
&lt;v-btn @click=&quot;setSelect()&quot;&gt;Set Select&lt;/v-btn&gt;
&lt;v-col cols=&quot;6&quot;&gt;
&lt;v-select
:value=&quot;select&quot;
:items=&quot;items&quot;
item-text=&quot;state&quot;
item-value=&quot;abbr&quot;
label=&quot;Select&quot;
persistent-hint
single-line
@change=&quot;setSelected&quot;
&gt;&lt;/v-select&gt;
&lt;/v-col&gt;
&lt;/v-row&gt;
&lt;/v-container&gt;
&lt;/v-app&gt;
&lt;/div&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月30日 00:46:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359034.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定