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

Vue中select下拉框的默認(rèn)選中項(xiàng)的三種情況解讀

 更新時(shí)間:2023年05月11日 14:58:19   作者:July++  
這篇文章主要介紹了Vue中select下拉框的默認(rèn)選中項(xiàng)的三種情況解讀,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

vue select下拉框的默認(rèn)選中項(xiàng)的三種情況

在Vue中 使用select下拉框 主要靠的是 v-model 來綁定選項(xiàng) option 的 value 值。

select下拉框在界面的展示,我們都希望看到框中有一個(gè)值 而不是空白,比如顯示 “請選擇” 或者 “全部” 的默認(rèn)值。

先來看效果圖

select默認(rèn)選中項(xiàng)

關(guān)于select選項(xiàng)的寫法 有三種情況

  • ①.寫在HTML中
  • ②.寫在JS一個(gè)數(shù)組中
  • ③.通過接口去獲取得到

我們直接上代碼:

第一種是option的值寫在HTML中

  <div id="app">
     <select name="status" id="status" v-model="selected">
         <option value="">請選擇</option>
         <option value="1">未處理</option>
         <option value="2">處理中</option>
          <option value="3">處理完成</option>
     </select>
   </div>
<script>
   new Vue({
       el:'#app',
       data:{
              selected:''  
              //默認(rèn)選中項(xiàng)的value值是什么  就給綁定的屬性什么值  這里默認(rèn)選中項(xiàng)請選擇的value值是空  我們就給綁定的屬性 select 一個(gè)空值
       },
   })
    </script>

第二種是option 選項(xiàng)內(nèi)容寫在JS中的

通過v-for來遍歷的:

<body>
    <div id="app">
     <select name="status" id="status" v-model="selected">
            <option :value="item.statusId" v-for="(item,index) in statusArr">{{item.statusVal}}</option>
     </select>
   </div>
</body>
<script>
   new Vue({
       el:'#app',
       data:{
              statusArr:[
                  {
                      statusId:'0',
                      statusVal:'請選擇'
                  },
                  {
                      statusId:'1',
                      statusVal:'未處理'
                  },
                  {
                      statusId:'2',
                      statusVal:'處理中'
                  },
                  {
                      statusId:'3',
                      statusVal:'處理完成'
                  },
              ],
              selected:''
       },
       created(){
           // 在組件創(chuàng)建之后,把默認(rèn)選中項(xiàng)的value值賦給綁定的屬性
           //如果沒有這句代碼,select中初始化會(huì)是空白的,默認(rèn)選中就無法實(shí)現(xiàn)
         this.selected = this.statusArr[0].statusId;
       }
   })
    </script>

第三種是option 選項(xiàng)內(nèi)容

通過接口去獲取 但是接口里沒有默認(rèn)選中項(xiàng)怎么辦呢?看代碼

<body>
    <div id="app">
     <select name="status" id="status" v-model="selected">
         <!-- 由于從接口獲取的select的下拉值沒有‘請選擇',所以我們要自己寫一個(gè) -->
        <option value="">請選擇</option> 
        <option :value="item.statusId" v-for="(item,index) in statusArr">{{item.statusVal}}</option>
     </select>
   </div>
</body>
<script>
   new Vue({
       el:'#app',
       data:{
              statusArr:[], //用來接收從接口里獲取出來的select下拉框里的值
              selected:''
       },
       getSelectInfo(){
           var url = "../monitor_admin_front/device/status"; //接口地址
           axios.get(url)
           .then(response => {
              if(response.data.retCode == 0){
                 this.statusArr = response.data.data; //將獲取出來的數(shù)據(jù)賦給定義的數(shù)組 以便于去循環(huán)遍歷
              }
           }) 
       },
       created(){
         this.getSelectInfo();
       }
   })
    </script>

vue中select默認(rèn)選中下拉選項(xiàng)第一條(舉例iview AutoComplete組件)

html中

  • 靜態(tài)

給對應(yīng)option元素直接添加selected屬性

<select> 
	<option value="0">one</option> 
	<option value="1">two</option> 
	<option value="2" selected>three</option> 
</select>
  • 動(dòng)態(tài)

option加載完成后給對應(yīng)元素添加selected屬性

$("option")[2].prop("selected","selected");

vue中

選中第一項(xiàng)同時(shí)并改變select綁定的value(正常業(yè)務(wù))

<template>
	<select v-model="value"> 
		<option value="0">New York</option> 
		<option value="1">London</option>
		<option value="2">Sydney</option>
		<option value="3">Ottawa</option>
	</select>
</template>
export default {
	data () {
    	return {
    		value: '0'	
    	}
    }
}

可模糊匹配,select輸入模糊匹配,默認(rèn)選中下拉選項(xiàng)中第一項(xiàng),但select綁定的value不變,即下圖所示:

可模糊匹配,select輸入模糊匹配,默認(rèn)選中下拉選項(xiàng)中第一項(xiàng),但select綁定的value不變

我的做法是

1.輸入值改變時(shí),監(jiān)聽change事件引起下拉選項(xiàng)的發(fā)生改變

2.判斷篩選后的下拉選項(xiàng)Dom元素中沒有選中項(xiàng),為第一項(xiàng)New York添加選中樣式;若有選中項(xiàng)則不操作

3.監(jiān)聽回車事件,如此時(shí)有回車事件,判斷下拉選項(xiàng)Dom中第一項(xiàng)有選中樣式則將value的值變更為選項(xiàng)中第一項(xiàng)的值,即this.value = ‘New York’,并將下拉選項(xiàng)框隱藏

4.我的實(shí)際列子是使用iviewUI中的AutoComplete組件做的(簡單舉個(gè)例子,只提供參考)

<template>
  <div id="search-input-component">
    <AutoComplete
      v-model="value"
      type="text"
      @keydown.enter.native.prevent="enterEvent"
      @on-change="change"
    >
      <Option
        v-for="item in changeList"
        :value="item.value"
        :key="item.value"
      >
        {{item.name}}
      </Option>
    </AutoComplete>
  </div>
</template>
<script>
export default {
  data () {
    return {
      value: '', // AutoComplete綁定的值
      valueLists: [
        // { value: '選項(xiàng)值', name: '選項(xiàng)名' }
      ], // 全部拉選項(xiàng)列表
      changeList: [] // 實(shí)時(shí)模糊匹配的下拉選項(xiàng)列表
    }
  },
  created () {
    this.getAllList()
  },
  methods: {
    // 獲取所有下拉選項(xiàng)
    getAllList () {
      // ...Data
      this.valueLists = Data
      this.changeList = Data
    },
    // 輸入change事件
    change (val) {
      // 模糊匹配邏輯,重新生成下拉選項(xiàng)列表
      this.changeList = []
      this.valueLists.map(item => {
        if (item.toUpperCase().indexOf(val.toUpperCase()) !== -1) {
          this.changeList.push(item)
        }
      })
      this.$nextTick(() => {
        // 下拉框中所有選項(xiàng)的Dom
        const ele = document.querySelectorAll('#search-input-component .ivu-select-dropdown.ivu-auto-complete .ivu-select-dropdown-list .ivu-select-item')
        let hasFocus = false // 初始賦值沒有選中項(xiàng)
        for(let i=0; i < ele.length; i++) {
          // 判斷有選中項(xiàng)
          if (ele[i].className.indexOf('ivu-select-item-focus') > -1) {
            hasFocus = true
            break
          }
        }
        // 判斷沒有選中項(xiàng),則選中第一項(xiàng)(添加選中樣式)
        if (!hasFocus && (ele[0].className.indexOf('ivu-select-item-focus') === -1)) {
          ele[0].classList += ' ivu-select-item-focus'
        }
      })
    },
    // 回車事件
    enterEvent (card, isUpdate) {
      const selectDom = document.querySelector('#search-input-component .ivu-select-dropdown.ivu-auto-complete')
      // 下拉選項(xiàng)有匹配到數(shù)據(jù) 并且 下拉選項(xiàng)沒有隱藏
      if (this.changeList.length && !selectDom.style.display) {
        // 下拉框中所有選項(xiàng)的Dom
        const items = document.querySelectorAll('#search-input-component .ivu-select-dropdown.ivu-auto-complete .ivu-select-dropdown-list .ivu-select-item')
        let hasFocus = false // 初始賦值沒有選中項(xiàng)
        for(let i=0; i < items.length; i++) {
          // 判斷有選中項(xiàng)
          if (items[i].className.indexOf('ivu-select-item-focus') > -1) {
            hasFocus = true
            break
          }
        }
        // 判斷沒有選中項(xiàng),則選中第一項(xiàng)
        if (!hasFocus) {
          this.value = this.changeList[0].cardId
        }
        selectDom.style.display = 'none' // 隱藏下拉框
      }
    }
  }
}
</script>

總結(jié)

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

相關(guān)文章

  • vue+element下拉列表默認(rèn)值問題

    vue+element下拉列表默認(rèn)值問題

    這篇文章主要介紹了vue+element下拉列表默認(rèn)值問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • vite+ts vite.config.ts使用path報(bào)錯(cuò)問題及解決

    vite+ts vite.config.ts使用path報(bào)錯(cuò)問題及解決

    這篇文章主要介紹了vite+ts vite.config.ts使用path報(bào)錯(cuò)問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • vue3關(guān)于RouterView插槽和過渡動(dòng)效

    vue3關(guān)于RouterView插槽和過渡動(dòng)效

    這篇文章主要介紹了vue3關(guān)于RouterView插槽和過渡動(dòng)效,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • VUE獲取Promise對象中PromiseResult中的數(shù)據(jù)(最新推薦)

    VUE獲取Promise對象中PromiseResult中的數(shù)據(jù)(最新推薦)

    如果想要在接口請求方法的外面拿到請求的結(jié)果,再做相關(guān)數(shù)據(jù)處理,往往我們拿到的卻是一個(gè)Promise對象,那么遇到這樣的問題如何解決呢,下面小編給大家?guī)砹薞UE?項(xiàng)目中?如何獲取Promise對象中的PromiseResult中的數(shù)據(jù)?,感興趣的朋友一起看看吧
    2023-10-10
  • vue3?使用defineExpose的實(shí)例詳解

    vue3?使用defineExpose的實(shí)例詳解

    這篇文章主要介紹了vue3?使用defineExpose的相關(guān)知識,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-03-03
  • 一文詳解如何在vue中實(shí)現(xiàn)文件預(yù)覽功能

    一文詳解如何在vue中實(shí)現(xiàn)文件預(yù)覽功能

    很多Vue項(xiàng)目中都需要PDF文件預(yù)覽功能,比如合同ERP,銷售CRM,內(nèi)部文檔CMS管理系統(tǒng),內(nèi)置PDF文件在線預(yù)覽功能,下面這篇文章主要給大家介紹了關(guān)于如何在vue中實(shí)現(xiàn)文件預(yù)覽功能的相關(guān)資料,需要的朋友可以參考下
    2022-10-10
  • vue3中使用scss加上scoped導(dǎo)致樣式失效問題

    vue3中使用scss加上scoped導(dǎo)致樣式失效問題

    這篇文章主要介紹了vue3中使用scss加上scoped導(dǎo)致樣式失效問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • vue-cli項(xiàng)目中使用echarts圖表實(shí)例

    vue-cli項(xiàng)目中使用echarts圖表實(shí)例

    在本篇文章里我們給大家分享了關(guān)于vue中使用echarts圖表的實(shí)現(xiàn)方法,有興趣的朋友們學(xué)習(xí)下。
    2018-10-10
  • v-for中動(dòng)態(tài)校驗(yàn)el-form表單項(xiàng)的實(shí)踐

    v-for中動(dòng)態(tài)校驗(yàn)el-form表單項(xiàng)的實(shí)踐

    在項(xiàng)目開發(fā)中,我們經(jīng)常會(huì)遇到表單保存的功能,本文主要介紹了v-for中動(dòng)態(tài)校驗(yàn)el-form表單項(xiàng)的實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧<BR>
    2022-05-05
  • ???????基于el-table和el-pagination實(shí)現(xiàn)數(shù)據(jù)的分頁效果

    ???????基于el-table和el-pagination實(shí)現(xiàn)數(shù)據(jù)的分頁效果

    本文主要介紹了???????基于el-table和el-pagination實(shí)現(xiàn)數(shù)據(jù)的分頁效果,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08

最新評論

元氏县| 灵宝市| 榆树市| 民权县| 丰镇市| 正镶白旗| 新兴县| 桂平市| 敦化市| 隆回县| 博乐市| 望谟县| 彭州市| 济宁市| 汨罗市| 平湖市| 灵寿县| 汽车| 拉孜县| 蒙山县| 益阳市| 方正县| 米脂县| 平舆县| 宝丰县| 迁安市| 梁平县| 定陶县| 永安市| 岳普湖县| 灵山县| 平遥县| 会东县| 陵川县| 孟州市| 金湖县| 荣昌县| 庆城县| 定襄县| 罗源县| 台山市|