欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue Element如何獲取select選擇框選擇的value和label

 更新時(shí)間:2025年01月30日 07:07:48   作者:孫 悟 空  
文章介紹了兩種使用Vue.js和ElementUI獲取select選擇框值的方法:一種是使用watch監(jiān)聽selectedValue的變化,另一種是使用@change事件,兩種方法都能實(shí)現(xiàn)獲取選擇的value和label

1 使用watch監(jiān)聽selectedValue的變化

可以使用Element UI中的v-model指令,將選中的值和對(duì)應(yīng)的標(biāo)簽存儲(chǔ)在data中的變量中

具體代碼如下:

<template>
  <el-select v-model="selectedValue" placeholder="請(qǐng)選擇">
    <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
    </el-option>
  </el-select>
  <div>
    <div>選擇的值:{{ selectedValue }}</div>
    <div>對(duì)應(yīng)的標(biāo)簽:{{ selectedLabel }}</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      options: [
        { value: 'option1', label: '選項(xiàng)1' },
        { value: 'option2', label: '選項(xiàng)2' },
        { value: 'option3', label: '選項(xiàng)3' }
      ],
      selectedValue: '',
      selectedLabel: ''
    };
  },
  watch: {
    selectedValue(newVal) {
      const option = this.options.find(item => item.value === newVal);
      this.selectedLabel = option ? option.label : '';
    }
  }
};
</script>

結(jié)果展示:

template中,v-model指令綁定了selectedValue變量,表示選中的值。

同時(shí),給<el-option>添加了v-for循環(huán)生成所有的選項(xiàng)。

當(dāng)選中的值改變時(shí),使用watch監(jiān)聽selectedValue的變化,通過(guò)find方法從options中找到選中的值對(duì)應(yīng)的選項(xiàng),并將標(biāo)簽存儲(chǔ)在selectedLabel變量中。

最后,將selectedValueselectedLabel顯示在頁(yè)面上。

2 @change事件獲取

2.1 只返回選擇的value

<template>
  <div>
    <el-select v-model="selectedValue" @change="getSelectValue">
      <el-option
        v-for="option in options"
        :key="option.value"
        :label="option.label"
        :value="option.value"
      >
      </el-option>
    </el-select>
  </div>
</template>
<script>
export default {
  data() {
    return {
      options: [
       	{ value: 'option1', label: '選項(xiàng)1' },
        { value: 'option2', label: '選項(xiàng)2' },
        { value: 'option3', label: '選項(xiàng)3' }
      ],
      selectedValue: '',
    };
  },
  methods: {
    getSelectValue(data) {
     console.log('value', data);
    },
  },
};
</script>

結(jié)果展示:

2.2 返回選擇的value和label

下面是一個(gè)使用@change獲取element選擇框的值和名稱的Vue示例代碼:

<template>
  <div>
    <el-select v-model="selectedOption" @change="handleOptionChange">
      <el-option
        v-for="option in options"
        :key="option.value"
        :label="option.label"
        :value="option.value"
      >
      </el-option>
    </el-select>
    <p>Selected Option: {{ selectedOption }}</p>
    <p>Selected Option Label: {{ selectedOptionLabel }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      options: [
       	{ value: 'option1', label: '選項(xiàng)1' },
        { value: 'option2', label: '選項(xiàng)2' },
        { value: 'option3', label: '選項(xiàng)3' }
      ],
      selectedOption: '',
      selectedOptionLabel: '',
    };
  },
  methods: {
    handleOptionChange() {
      this.selectedOptionLabel = this.options.find(
        (option) => option.value === this.selectedOption
      ).label;
    },
  },
};
</script>

結(jié)果展示:

在這個(gè)示例代碼中,我們首先定義了一個(gè)el-select元素,并使用v-model指令綁定了一個(gè)selectedOption變量,這個(gè)變量將用于存儲(chǔ)用戶選擇的選項(xiàng)的值。

接著,我們?cè)?code>el-select元素上添加了一個(gè)@change事件監(jiān)聽器,當(dāng)用戶在選擇框中選擇一個(gè)選項(xiàng)時(shí),該事件監(jiān)聽器會(huì)被觸發(fā)。

handleOptionChange方法是@change事件監(jiān)聽器的處理函數(shù),它通過(guò)使用find方法查找用戶選擇的選項(xiàng)的標(biāo)簽,并將其存儲(chǔ)在selectedOptionLabel變量中。

最后,我們?cè)谀0逯袑?code>selectedOption和selectedOptionLabel變量的值顯示出來(lái)。

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue如何實(shí)現(xiàn)未登錄不能訪問(wèn)某些頁(yè)面

    vue如何實(shí)現(xiàn)未登錄不能訪問(wèn)某些頁(yè)面

    這篇文章主要介紹了vue如何實(shí)現(xiàn)未登錄不能訪問(wèn)某些頁(yè)面問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • vue中標(biāo)簽自定義屬性的使用及說(shuō)明

    vue中標(biāo)簽自定義屬性的使用及說(shuō)明

    這篇文章主要介紹了vue中標(biāo)簽自定義屬性的使用及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • vue中計(jì)算屬性computed和普通屬性method的區(qū)別小結(jié)

    vue中計(jì)算屬性computed和普通屬性method的區(qū)別小結(jié)

    Vue.js中Computed和Methods是兩種常用的數(shù)據(jù)處理方式,本文主要介紹了vue中計(jì)算屬性computed和普通屬性method的區(qū)別小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-06-06
  • Vue中$router與?$route的區(qū)別詳解

    Vue中$router與?$route的區(qū)別詳解

    這篇文章主要介紹了Vue中$router與?$route的區(qū)別詳解,文章圍繞主題展開詳細(xì)的內(nèi)容戒殺,具有一定的參考價(jià)值,需要的朋友可以參考一下
    2022-09-09
  • vue中keep-alive組件的用法示例

    vue中keep-alive組件的用法示例

    眾所周知keep-alive是Vue提供的一個(gè)抽象組件,主要是用來(lái)對(duì)組件進(jìn)行緩存,從而做到節(jié)省性能,這篇文章主要給大家介紹了關(guān)于vue中keep-alive組件用法的相關(guān)資料,需要的朋友可以參考下
    2021-05-05
  • Vue項(xiàng)目實(shí)現(xiàn)換膚功能的一種方案分析

    Vue項(xiàng)目實(shí)現(xiàn)換膚功能的一種方案分析

    這篇文章主要介紹了Vue項(xiàng)目實(shí)現(xiàn)換膚功能的一種方案分析,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Vue移動(dòng)端下拉加載更多數(shù)據(jù)onload實(shí)現(xiàn)方法淺析

    Vue移動(dòng)端下拉加載更多數(shù)據(jù)onload實(shí)現(xiàn)方法淺析

    這篇文章主要介紹了Vue移動(dòng)端下拉加載更多數(shù)據(jù)onload實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • Vue路由傳參詳細(xì)介紹

    Vue路由傳參詳細(xì)介紹

    這篇文章主要介紹了Vue路由傳參的兩種方式query和params,介紹了query和params區(qū)別與總結(jié),結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-09-09
  • vue實(shí)現(xiàn)計(jì)算器功能

    vue實(shí)現(xiàn)計(jì)算器功能

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • vue在自定義組件中使用v-model進(jìn)行數(shù)據(jù)綁定的方法

    vue在自定義組件中使用v-model進(jìn)行數(shù)據(jù)綁定的方法

    這篇文章主要介紹了vue在自定義組件中使用v-model進(jìn)行數(shù)據(jù)綁定的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03

最新評(píng)論