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)文章
vite+vue3項目集成ESLint與prettier的過程詳解
這篇文章主要介紹了vite+vue3項目中集成ESLint與prettier的相關(guān)知識,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09
結(jié)合Vue控制字符和字節(jié)的顯示個數(shù)的示例
這篇文章主要介紹了結(jié)合Vue控制字符和字節(jié)的顯示個數(shù)的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
vue實現(xiàn)樣式之間的切換及vue動態(tài)樣式的實現(xiàn)方法
這篇文章主要介紹了vue中如何實現(xiàn)樣式之間的切換及vue動態(tài)樣式的實現(xiàn)方法,本文給大家介紹的非常詳細(xì),具有參考借鑒價值,需要的朋友可以參考下2017-12-12
vue動態(tài)生成新表單并且添加驗證校驗規(guī)則方式
這篇文章主要介紹了vue動態(tài)生成新表單并且添加驗證校驗規(guī)則方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10

