Vue3 表單輸入綁定的操作方法
1. 基本用法(v-model 語(yǔ)法糖)
在前端處理表單時(shí),我們常常需要將表單輸入框的內(nèi)容同步給 JavaScript 中相應(yīng)的變量。手動(dòng)連接值綁定和更改事件監(jiān)聽(tīng)器可能會(huì)很麻煩。比如:
<template>
<input
:value="text"
@input="event => handleInput(event.target.value)">
</template>
<script setup>
import { ref } from 'vue';
let text = ref('')
function handleInput(newText) {
text.value = newText;
console.log('text:', text.value); // 用于測(cè)試數(shù)據(jù)是否發(fā)生了改變
}
</script>
<style scoped>
</style>
1.1 input 文本框
因?yàn)?Vue 中會(huì)大量使用到雙向數(shù)據(jù)綁定,因此使用了 v-model 語(yǔ)法糖,為我們簡(jiǎn)化了這一過(guò)程:
<input v-model="text">
<template>
<input v-model="text" @input="changeInput">
</template>
<script setup>
import { ref } from 'vue';
let text = ref('')
// 不手動(dòng)修改,只是監(jiān)聽(tīng)文本框的值變化
function changeInput() {
console.log('text的值發(fā)生了改變', text.value);
}
</script>
<style scoped>
</style>
v-model 還可以用于各種不同類(lèi)型的輸入,<textarea>、<select> 元素。它會(huì)根據(jù)所使用的元素自動(dòng)使用對(duì)應(yīng)的 DOM 屬性和事件組合:
- 文本類(lèi)型的
<input>和<textarea>元素會(huì)綁定value property并偵聽(tīng)input事件; <input type="checkbox">和<input type="radio">會(huì)綁定checked property并偵聽(tīng)change事件;<select>會(huì)綁定value property并偵聽(tīng)change事件。
1.2 textarea 多行文本
<template>
<textarea cols="30" rows="10" v-model="textContent"></textarea>
<p>你當(dāng)前輸入的內(nèi)容為:{{ textContent }}</p>
</template>
<script setup>
import { ref } from 'vue'
const textContent = ref('')
</script>
<style scoped></style>
1.3 checkbox 復(fù)選框
1.3.1 單一復(fù)選框
1.3.1.1 基本使用方式
單一的復(fù)選框,綁定布爾類(lèi)型值:
<template>
<div>
<input type="checkbox" v-model="checked" />
<label for="checkbox">{{ checked }}</label>
</div>
</template>
<script setup>
import { ref } from 'vue'
const checked = ref(true)
</script>
<style scoped></style>
點(diǎn)擊復(fù)選框,進(jìn)行切換

1.3.1.2 真假值自定義
1.3.1.1 的例子中,復(fù)選框默認(rèn)真假值為布爾值 true 和 false,但是這其實(shí)可以進(jìn)行自定義:
<template>
<div>
<input
type="checkbox"
v-model="checked"
:true-value="1"
:false-value="0" />
<!-- 這里的label通常為固定文本,修改為checked只是為了展示數(shù)據(jù)變化 -->
<label for="checkbox">{{ checked }}</label>
</div>
</template>
<script setup>
import { ref } from 'vue'
const checked = ref(1)
</script>
<style scoped></style>

1.3.2 多個(gè)復(fù)選框
對(duì)于多個(gè)復(fù)選框,我們通常將其多個(gè)選項(xiàng)綁定在一個(gè)數(shù)組或者集合中,方便遍歷展示:
<template>
<div v-for="(item, index) in arr" :key="index">
<label for="item.id">{{ item.title }}</label>
<input type="checkbox" v-model="hobby" :id="item.id" :value="item.value" />
</div>
<p>{{ message }}</p>
</template>
<script setup>
import { ref, computed } from 'vue'
const hobby = ref([])
const arr = ref([
{ id: 'swim', title: '游泳', value: '游個(gè)泳' },
{ id: 'run', title: '跑步', value: '跑個(gè)步' },
{ id: 'game', title: '游戲', value: '玩?zhèn)€游戲' },
{ id: 'music', title: '音樂(lè)', value: '聽(tīng)個(gè)音樂(lè)' },
{ id: 'movie', title: '電影', value: '看個(gè)電影' }
])
const message = computed(() => {
// 根據(jù) hobby 的值進(jìn)行二次計(jì)算
if (hobby.value.length === 0) return '請(qǐng)選擇愛(ài)好'
else return `您選擇了${hobby.value.join('、')}`
})
</script>
<style scoped></style>
1.4 radio 單選框
<template>
<input type="radio" id="male" v-model="gender" value="male" />
<label for="male">男</label>
<input type="radio" id="female" v-model="gender" value="female" />
<label for="female">女</label>
</template>
<script setup>
import { ref } from 'vue'
const gender = ref('male')
</script>
<style scoped></style>
1.5 select 選擇器(多選時(shí)需要按住ctrl鍵)
<template>
<!-- 下拉列表列表是單選的話(huà),v-model 綁定的值是一個(gè)字符串,這個(gè)字符串是 option 的 value 值 -->
<select v-model="hometown1">
<option value="" disabled>請(qǐng)選擇</option>
<option v-for="(item, index) in hometownList" :key="index" :value="item.value">
{{ item.label }}
</option>
</select>
<p>您選擇的家鄉(xiāng)為:{{ hometown1 }}</p>
<!-- 如果下拉列表是多選的話(huà),v-model 綁定的值是一個(gè)數(shù)組,這個(gè)數(shù)組是 option 的 value 值組成的數(shù)組 -->
<select v-model="hobbies" multiple>
<option label="" disabled>請(qǐng)選擇</option>
<option v-for="(item, index) in hobbyList" :key="index" :value="item.value">
{{ item.label }}
</option>
</select>
<p>按住 Ctrl (windows) / Command (Mac) 按鈕來(lái)選擇多個(gè)選項(xiàng)。</p>
<p>您選擇的興趣為:{{ hobbies }}</p>
</template>
<script setup>
import { ref } from 'vue'
const hometown1 = ref('')
const hobbies = ref([])
const hometownList = ref([
{ value: '成都', label: '四川' },
{ value: '帝都', label: '北京' },
{ value: '魔都', label: '上海' },
{ value: '妖都', label: '廣州' },
{ value: '陪都', label: '重慶' }
])
const hobbyList = ref([
{ value: 'basketball', label: '籃球' },
{ value: 'football', label: '橄欖球' },
{ value: 'soccer', label: '足球' },
{ value: 'baseball', label: '排球' },
])
</script>
<style scoped></style>
2. 表單修飾符
2.1 課外拓展(input 和 change 事件的區(qū)別)
2.1.1 觸發(fā)時(shí)機(jī)
- input事件:用戶(hù)每次輸入(包括鍵盤(pán)輸入、粘貼、語(yǔ)音輸入等)或通過(guò)腳本修改值時(shí)實(shí)時(shí)觸發(fā)。
- change事件:元素失去焦點(diǎn)且內(nèi)容發(fā)生變化時(shí)觸發(fā)(如點(diǎn)擊其他區(qū)域、選擇下拉框等),不會(huì)因輸入過(guò)程中頻繁觸發(fā)。
2.1.2 適用場(chǎng)景
- input事件:適合需要實(shí)時(shí)反饋的場(chǎng)景,如搜索聯(lián)想、密碼強(qiáng)度檢測(cè)、字?jǐn)?shù)統(tǒng)計(jì)等。
- change事件:通常用于表單提交前的最終驗(yàn)證,或需要離開(kāi)輸入框后才能確認(rèn)數(shù)據(jù)有效性的場(chǎng)景。
2.2 表單修飾符的使用
.lazy:默認(rèn)情況下,v-model會(huì)在每次 input 事件觸發(fā)時(shí)就更新數(shù)據(jù),lazy修飾符可以改為change事件觸發(fā)后才更新數(shù)據(jù);.number:將用戶(hù)輸入的內(nèi)容從字符串轉(zhuǎn)為number類(lèi)型;.trim:默認(rèn)自動(dòng)去除用戶(hù)輸入內(nèi)容中兩端的空格。
<template>
<!-- lazy修飾符演示 -->
<input type="text" v-model.lazy="mess1" />
<p>你輸入的是:{{ mess1 }}</p>
<p>類(lèi)型為{{ typeof mess1 }}</p>
<p>長(zhǎng)度為{{ mess1.length }}</p>
<!-- number修飾符演示 -->
<input type="text" v-model.number="mess2" />
<p>你輸入的是:{{ mess2 }}</p>
<p>類(lèi)型為{{ typeof mess2 }}</p>
<p>長(zhǎng)度為{{ mess2.length }}</p>
<!-- trim修飾符演示 -->
<input type="text" v-model.trim="mess3" />
<p>你輸入的是:{{ mess3 }}</p>
<p>類(lèi)型為{{ typeof mess3 }}</p>
<p>長(zhǎng)度為{{ mess3.length }}</p>
</template>
<script setup>
import { ref } from 'vue'
const mess1 = ref('')
const mess2 = ref('')
const mess3 = ref('')
</script>
<style scoped></style>到此這篇關(guān)于Vue3 表單輸入綁定的文章就介紹到這了,更多相關(guān)vue表單輸入綁定內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue中jsencrypt與base64加密解密的實(shí)用流程
vue項(xiàng)目里面使用到的加密和解密的方法,本文主要介紹了vue中jsencrypt與base64加密解密的實(shí)用流程,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10
element input輸入框自動(dòng)獲取焦點(diǎn)的實(shí)現(xiàn)
本文主要介紹了element input輸入框自動(dòng)獲取焦點(diǎn)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
Vue項(xiàng)目中使用better-scroll實(shí)現(xiàn)菜單映射功能方法
這篇文章主要介紹了Vue項(xiàng)目中使用better-scroll實(shí)現(xiàn)菜單映射功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
在vue中根據(jù)光標(biāo)的顯示與消失實(shí)現(xiàn)下拉列表
這篇文章主要介紹了在vue中根據(jù)光標(biāo)的顯示與消失實(shí)現(xiàn)下拉列表,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
vue實(shí)現(xiàn)短信驗(yàn)證碼登錄功能(流程詳解)
無(wú)論是移動(dòng)端還是pc端登錄或者注冊(cè)界面都會(huì)見(jiàn)到手機(jī)驗(yàn)證碼登錄這個(gè)功能,輸入手機(jī)號(hào),得到驗(yàn)證碼,這篇文章主要介紹了基于vue實(shí)現(xiàn)短信驗(yàn)證碼登錄功能,需要的朋友可以參考下2019-12-12

