Vue獲取input值的四種常用方法
1. v-model 表單輸入綁定
//使用v-model創(chuàng)建雙向數(shù)據(jù)綁定, 用來(lái)監(jiān)聽(tīng)用戶的輸入事件以更新數(shù)據(jù),并對(duì)一些極端場(chǎng)景進(jìn)行一些特殊處理
<template>
<div>
<input class="login-input" type="text" v-model="username" placeholder="請(qǐng)輸入賬號(hào)">
<input class="login-input" type="password" v-model="password" placeholder="請(qǐng)輸入密碼">
<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)聽(tīng)輸入框
//輸入框只要輸入的值變化了就會(huì)觸發(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)聽(tīng)輸入框
//輸入框失去焦點(diǎn)時(shí),輸入的值發(fā)生了變化,就會(huì)觸發(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í)例的數(shù)據(jù)屬性上,然后通過(guò)判斷該數(shù)據(jù)屬性的值來(lái)判斷輸入框是否有值。例如:
<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('輸入框?yàn)榭?)
}
}
}
}
</script>- 使用ref引用:給輸入框添加一個(gè)ref屬性,然后通過(guò)$refs來(lái)獲取輸入框元素的引用,再通過(guò)判斷輸入框元素的value屬性來(lái)判斷輸入框是否有值。例如:
<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('輸入框?yàn)榭?)
}
}
}
}
</script>總結(jié)
到此這篇關(guān)于Vue獲取input值的四種常用方法的文章就介紹到這了,更多相關(guān)Vue獲取input值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue單頁(yè)面在微信下只能分享落地頁(yè)的解決方案
這篇文章主要介紹了vue單頁(yè)面在微信下只能分享落地頁(yè)的解決方案,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04
vite+vue3項(xiàng)目集成ESLint與prettier的過(guò)程詳解
這篇文章主要介紹了vite+vue3項(xiàng)目中集成ESLint與prettier的相關(guān)知識(shí),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09
結(jié)合Vue控制字符和字節(jié)的顯示個(gè)數(shù)的示例
這篇文章主要介紹了結(jié)合Vue控制字符和字節(jié)的顯示個(gè)數(shù)的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
vue實(shí)現(xiàn)樣式之間的切換及vue動(dòng)態(tài)樣式的實(shí)現(xiàn)方法
這篇文章主要介紹了vue中如何實(shí)現(xiàn)樣式之間的切換及vue動(dòng)態(tài)樣式的實(shí)現(xiàn)方法,本文給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-12-12
vue動(dòng)態(tài)生成新表單并且添加驗(yàn)證校驗(yàn)規(guī)則方式
這篇文章主要介紹了vue動(dòng)態(tài)生成新表單并且添加驗(yàn)證校驗(yàn)規(guī)則方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10

