vue獲取input值的三種常用寫法
1. v-model 表單輸入綁定
使用v-model創(chuàng)建雙向數據綁定, 用來監(jiān)聽用戶的輸入事件以更新數據,并對一些極端場景進行一些特殊處理。
?? ?<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 調用 search 數據實時獲取通過 event.currentTarget.value 獲取到
?? ?<template> ?? ? ?<div class="class"> ?? ? ? ?<div> ?? ? ? ? ?<input type="text" @keyup.enter="search" @input="search($event)"/> ?? ? ? ?</div> ?? ? ?</div> ?? ?</template>
?? ?<script>
? ? export default {
? ? ? name: "search",
? ? ? data() {
? ? ? },
? ? ? methods: {
?? ? ? ? ? ?search(event){
?? ? ? ? ? ? ?console.log(event.currentTarget.value)
?? ? ? ? ? ?}
? ? ? ?? ?}
? ? }
? ?</script>3. ref 獲取數據
這種方式類似于原生DOM,但是ref獲取數據更方便
?? ?<template> ?? ? ?<div class="class"> ?? ? ? ? ?<input type="text" ref="getValue" /> ?? ? ? ? ?<button @click="subbmitButton">獲取表單數據</button> ?? ? ?</div> ?? ?</template>
?? ?<script>
? ? export default {
? ? ? name: "page",
? ? ? data() {
? ? ? },
? ? ? methods: {
?? ? ? ? ? ?subbmitButton(){
?? ? ? ? ? ? ?console.log(this.$refs.getValue.value)
?? ? ? ? ? ?}
? ? ? ?? ?}
? ? }
? </script>vue收集input[type=“checkbox”]的值
input[type=“checkbox”],勾選or不勾選
要控制input[type=“checkbox”]勾選或不勾選,有以下兩種方式,
v-model="isDone"。isDone為true時,勾選;isDone為false時,不勾選:checked="isDone"。isDone為true時,勾選,isDone為false時,不勾選
注意哈??!此時isDone必須初始化為布爾值(或者可布爾化的類型,如字符串,數值,非0即1)

v-model
<body>
<div id="root">
<input type="checkbox" v-model="isDone"/>已經完成
<button @click="handleClick">勾選/去勾選</button>
</div>
<script>
Vue.config.productionTip = false;
new Vue({
el:"#root",
data(){
return {
isDone:false
}
},
methods: {
handleClick(){
this.isDone = !this.isDone;
}
},
watch:{
isDone:{
immediate:true,
handler(newValue,oldValue){
console.log(newValue,oldValue);
}
}
}
})
</script>
</body>
:checked
<body>
<div id="root">
<input type="checkbox" :checked="isDone"/>已經完成
<button @click="handleClick">勾選/去勾選</button>
</div>
<script>
Vue.config.productionTip = false;
new Vue({
el:"#root",
data(){
return {
isDone:false
}
},
methods: {
handleClick(){
this.isDone = !this.isDone;
}
},
watch:{
isDone:{
immediate:true,
handler(newValue,oldValue){
console.log(newValue,oldValue);
}
}
}
})
</script>
</body>
input[type=“checkbox”]多個時,哪些被選中
多個input[type="checkbox"]時,想知道哪些被選中,使用v-model,如v-model="hobbies"。
注意哈??!此時hobbies必須初始化為數組。
<body>
<div id="root">
<label><input type="checkbox" name="hobby" value="football" v-model="hobbies"/>足球</label><br/>
<label><input type="checkbox" name="hobby" value="basketball" v-model="hobbies"/>籃球</label><br/>
<label><input type="checkbox" name="hobby" value="tennis" v-model="hobbies"/>網球</label>
</div>
<script>
Vue.config.productionTip = false;
new Vue({
el:"#root",
data(){
return {
hobbies:[]
}
},
watch:{
hobbies:{
immediate:true,
handler(newValue){
console.log(newValue);
}
}
}
})
</script>
</body>

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue中el-date-picker限制選擇7天內&禁止內框選擇
項目中需要選擇時間范圍,并且只能選擇當前日期之后的七天,本文就來介紹了vue中el-date-picker限制選擇7天內&禁止內框選擇,具有一定的參考價值,感興趣的可以了解一下2023-12-12
Vue3 defineExpose要在方法聲明定義以后使用的教程
這篇文章主要介紹了Vue3 defineExpose要在方法聲明定義以后使用的教程,本文結合實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-02-02
詳解webpack打包vue項目之后生成的dist文件該怎么啟動運行
這篇文章主要介紹了詳解webpack打包vue項目之后生成的dist文件該怎么啟動運行,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09
vue2.0如何實現(xiàn)echarts餅圖(pie)效果展示
這篇文章主要介紹了vue2.0如何實現(xiàn)echarts餅圖(pie)效果展示,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10

