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

Vue獲取input值的四種常用方法

 更新時間:2023年09月17日 10:00:45   作者:凡大來啦  
Vue是一種流行的Web開發(fā)框架,它提供了一個雙向綁定的語法糖。在Vue中,我們可以很容易地獲取頁面上的數(shù)據(jù),并且可以實時的響應(yīng)其變化,這篇文章主要給大家介紹了關(guān)于Vue獲取input值的四種常用方法,需要的朋友可以參考下

1. v-model 表單輸入綁定

//使用v-model創(chuàng)建雙向數(shù)據(jù)綁定, 用來監(jiān)聽用戶的輸入事件以更新數(shù)據(jù),并對一些極端場景進行一些特殊處理	
<template>
    <div>
	    <input class="login-input" type="text"  v-model="username" placeholder="請輸入賬號">
	    <input class="login-input" type="password" v-model="password" placeholder="請輸入密碼">
		<div class="login-button" @click="login" type="submit">登陸</div>
	</div>
</template>
<script>
    export default {
       name: 'Login',
       data() {
            return {
                username: '',
                password: ''
            }
        },
        methods: {
            login() {
                   console.log(this.username)
                   console.log(this.password)
            }
        }
    }
<script/>
 

2. @input 監(jiān)聽輸入框

//輸入框只要輸入的值變化了就會觸發(fā) input 調(diào)用 search	
<template>
	<div class="class">
		<div>
			<input type="text" @input="search"/>
		</div>
	</div>
</template>
<script>
	export default {
		name: "Search",
		data() {
		},
		methods: {
			search(event){
				console.log( event.currentTarget.value )
			}
		}
	}
</script>

3. @change 監(jiān)聽輸入框

//輸入框失去焦點時,輸入的值發(fā)生了變化,就會觸發(fā) change 事件
<template>
	<div class="class">
		<div>
			<input type="text" @change="search"/>
		</div>
	</div>
</template>
<script>
	export default {
		name: "Search",
		data() {
		},
		methods: {
			search(event){
				console.log( event.target.value )
			}
		}
	}
</script>

4. ref 獲取數(shù)據(jù)

//這種方式類似于原生DOM,但是ref獲取數(shù)據(jù)更方便
<template>
	<div class="class">
		<input type="text" ref="inputDom" />
		<button @click="subbmitButton">獲取表單數(shù)據(jù)</button>
	</div>
</template>
<script>
    export default {
		name: "Page",
		data() {
		},
		methods: {
			subbmitButton(){
				console.log( this.$refs.inputDom.value )
			}
		}
	}
</script>

附:vue如何判斷輸入框是否有值

在Vue中判斷輸入框是否有值的方法有多種。以下是其中兩種常用的方法:

  • 綁定v-model指令:將輸入框的值綁定到Vue實例的數(shù)據(jù)屬性上,然后通過判斷該數(shù)據(jù)屬性的值來判斷輸入框是否有值。例如:
<template>
  <input type="text" v-model="inputValue" />
  <button @click="checkInput">檢查輸入框是否有值</button>
</template>
<script>
export default {
  data() {
    return {
      inputValue: ''
    }
  },
  methods: {
    checkInput() {
      if (this.inputValue) {
        console.log('輸入框有值')
      } else {
        console.log('輸入框為空')
      }
    }
  }
}
</script>
  • 使用ref引用:給輸入框添加一個ref屬性,然后通過$refs來獲取輸入框元素的引用,再通過判斷輸入框元素的value屬性來判斷輸入框是否有值。例如:
<template>
  <input type="text" ref="myInput" />
  <button @click="checkInput">檢查輸入框是否有值</button>
</template>
<script>
export default {
  methods: {
    checkInput() {
      if (this.$refs.myInput.value) {
        console.log('輸入框有值')
      } else {
        console.log('輸入框為空')
      }
    }
  }
}
</script>

總結(jié) 

到此這篇關(guān)于Vue獲取input值的四種常用方法的文章就介紹到這了,更多相關(guān)Vue獲取input值內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 如何使用Vuex+Vue.js構(gòu)建單頁應(yīng)用

    如何使用Vuex+Vue.js構(gòu)建單頁應(yīng)用

    這篇文章主要教大家如何使用Vuex+Vue.js構(gòu)建單頁應(yīng)用,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • 計算屬性和偵聽器詳情

    計算屬性和偵聽器詳情

    這篇文章主要介紹了計算屬性和偵聽器,文章以介紹計算屬性、偵聽器的相關(guān)資料展開詳細(xì)內(nèi)容,需要的朋友可以參考一下,希望對你有所幫助
    2021-11-11
  • 關(guān)于Vue單頁面骨架屏實踐記錄

    關(guān)于Vue單頁面骨架屏實踐記錄

    這篇文章主要給大家介紹了關(guān)于Vue單頁面骨架屏實踐的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。
    2017-12-12
  • 基于axios在vue中的使用

    基于axios在vue中的使用

    這篇文章主要介紹了關(guān)于axios在vue中的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • vue3+vite中使用import.meta.glob的操作代碼

    vue3+vite中使用import.meta.glob的操作代碼

    在vue2的時候,我們一般引入多個js或者其他文件,一般使用? require.context 來引入多個不同的文件,但是vite中是不支持 require的,他推出了一個功能用import.meta.glob來引入多個,單個的文件,下面通過本文介紹vue3+vite中使用import.meta.glob,需要的朋友可以參考下
    2022-11-11
  • vue element實現(xiàn)表格合并行數(shù)據(jù)

    vue element實現(xiàn)表格合并行數(shù)據(jù)

    這篇文章主要為大家詳細(xì)介紹了vue element實現(xiàn)表格合并行數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • Vue3+Element-plus項目自動導(dǎo)入報錯的解決方案

    Vue3+Element-plus項目自動導(dǎo)入報錯的解決方案

    vue3出來一段時間了,element也更新了版本去兼容vue3,下面這篇文章主要給大家介紹了關(guān)于Vue3+Element-plus項目自動導(dǎo)入報錯的解決方案,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-07-07
  • 講解vue-router之什么是編程式路由

    講解vue-router之什么是編程式路由

    編程式路由在我們的項目使用過程中最常用的的方法了。這篇文章主要介紹了講解vue-router之什么是編程式路由,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • Vue3中Hooks封裝的技巧詳解

    Vue3中Hooks封裝的技巧詳解

    這篇文章主要來和大家分享一些關(guān)于 Vue3中Hooks封裝的技巧,希望能夠為大家在 Vue 3 項目中更好地利用 Hooks 提供一些思路和實踐經(jīng)驗
    2023-12-12
  • vue監(jiān)聽瀏覽器的后退和刷新事件,阻止默認(rèn)的事件方式

    vue監(jiān)聽瀏覽器的后退和刷新事件,阻止默認(rèn)的事件方式

    這篇文章主要介紹了vue監(jiān)聽瀏覽器的后退和刷新事件,阻止默認(rèn)的事件方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10

最新評論

新津县| 安徽省| 丰顺县| 儋州市| 东明县| 连城县| 莲花县| 漳浦县| 靖宇县| 桑日县| 山东| 河池市| 闻喜县| 西林县| 女性| 洛阳市| 石渠县| 青铜峡市| 南宫市| 句容市| 泗洪县| 康平县| 丰镇市| 南涧| 新巴尔虎左旗| 青田县| 太湖县| 天津市| 垫江县| 淅川县| 扶沟县| 五寨县| 长寿区| 邵阳市| 江源县| 宁强县| 监利县| 察哈| 集安市| 民乐县| 镇原县|