vue獲取input值的三種常用寫法
1. v-model 表單輸入綁定
使用v-model創(chuàng)建雙向數(shù)據(jù)綁定, 用來監(jiān)聽用戶的輸入事件以更新數(shù)據(jù),并對一些極端場景進(jì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 調(diào)用 search 數(shù)據(jù)實時獲取通過 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 獲取數(shù)據(jù)
這種方式類似于原生DOM,但是ref獲取數(shù)據(jù)更方便
?? ?<template> ?? ? ?<div class="class"> ?? ? ? ? ?<input type="text" ref="getValue" /> ?? ? ? ? ?<button @click="subbmitButton">獲取表單數(shù)據(jù)</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時,不勾選
注意哈?。〈藭risDone必須初始化為布爾值(或者可布爾化的類型,如字符串,數(shù)值,非0即1)
v-model
<body> <div id="root"> <input type="checkbox" v-model="isDone"/>已經(jīng)完成 <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"/>已經(jīng)完成 <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必須初始化為數(shù)組。
<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"/>網(wǎng)球</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>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue中el-date-picker限制選擇7天內(nèi)&禁止內(nèi)框選擇
項目中需要選擇時間范圍,并且只能選擇當(dāng)前日期之后的七天,本文就來介紹了vue中el-date-picker限制選擇7天內(nèi)&禁止內(nèi)框選擇,具有一定的參考價值,感興趣的可以了解一下2023-12-12Vue3 defineExpose要在方法聲明定義以后使用的教程
這篇文章主要介紹了Vue3 defineExpose要在方法聲明定義以后使用的教程,本文結(jié)合實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-02-02詳解webpack打包vue項目之后生成的dist文件該怎么啟動運行
這篇文章主要介紹了詳解webpack打包vue項目之后生成的dist文件該怎么啟動運行,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09vue等待數(shù)據(jù)渲染完成后執(zhí)行下一個方法問題
這篇文章主要介紹了vue等待數(shù)據(jù)渲染完成后執(zhí)行下一個方法問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12vue2.0如何實現(xiàn)echarts餅圖(pie)效果展示
這篇文章主要介紹了vue2.0如何實現(xiàn)echarts餅圖(pie)效果展示,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10