最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

1 使用watch監(jiān)聽(tīng)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)聽(tīng)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)聽(tīng)器,當(dāng)用戶在選擇框中選擇一個(gè)選項(xiàng)時(shí),該事件監(jiān)聽(tīng)器會(huì)被觸發(fā)。

handleOptionChange方法是@change事件監(jiān)聽(tīng)器的處理函數(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)文章

最新評(píng)論

石渠县| 黑龙江省| 舟山市| 宁晋县| 石城县| 凤山县| 札达县| 六枝特区| 布尔津县| 江安县| 德安县| 青田县| 囊谦县| 利辛县| 岑巩县| 清原| 军事| 彭州市| 通化市| 昆山市| 胶南市| 芜湖市| 高密市| 富锦市| 永嘉县| 隆尧县| 南和县| 黄龙县| 勃利县| 玛纳斯县| 红原县| 新干县| 南部县| 蒙自县| 昔阳县| 文昌市| 柯坪县| 田阳县| 皋兰县| 嘉兴市| 栾川县|