Vue使用v-model收集各種表單數(shù)據(jù)、過(guò)濾器的示例詳解
1. 使用v-model收集各種表單數(shù)據(jù)
- 若<input type=“text”/>,則v-model收集的是value值,用戶輸入的就是value值
- 若<input type=“radio”/>,則v-model收集的是value值,所以要給標(biāo)簽配置value值
- 若:<input type=“checkbox”/>
- 沒(méi)有配置input的value屬性,那么收集的就是checked(是布爾值),勾選一個(gè)所有的都會(huì)被勾選
- 配置input的value屬性:
- v-model的初始值是非數(shù)組,那么收集的就是checked(是布爾值)
- v-model的初始值是數(shù)組,那么收集的的就是value組成的數(shù)組
v-model的三個(gè)修飾符:
- lazy:失去焦點(diǎn)再收集數(shù)據(jù)
- number:輸入字符串轉(zhuǎn)為有效的數(shù)字
- trim:去除輸入的首尾空格
使用示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javaScript" src="../js/vue.js"></script>
</head>
<body>
<div id="root">
<!-- prevent: 點(diǎn)擊submit不進(jìn)行跳轉(zhuǎn) -->
<form @submit.prevent="printInfo">
賬號(hào):<input type="text" v-model.trim="userInfo.account"> <br/><br/>
密碼:<input type="password" v-model="userInfo.password"> <br/><br/>
<!-- type="number": 現(xiàn)在文本框只能輸入數(shù)字,但vm保存的還是字符串 -->
年齡:<input type="number" v-model.number="userInfo.age"> <br/><br/>
性別:
男<input type="radio" name="sex" v-model="userInfo.sex" value="male">
女<input type="radio" name="sex" v-model="userInfo.sex" value="female"> <br/><br/>
愛(ài)好:
學(xué)習(xí)<input type="checkbox" v-model="userInfo.hobby" value="study">
吃飯<input type="checkbox" v-model="userInfo.hobby" value="eat">
<br/><br/>
所屬校區(qū)
<select v-model="userInfo.city">
<option value="">請(qǐng)選擇校區(qū)</option>
<option value="beijing">北京</option>
<option value="shanghai">上海</option>
</select>
<br/><br/>
其他信息:
<textarea v-model.lazy="userInfo.other"></textarea> <br/><br/>
<input type="checkbox" v-model="userInfo.agree">閱讀并接受<a rel="external nofollow" >《用戶協(xié)議》</a>
<button>提交</button>
</form>
</div>
</body>
<script type="text/javascript">
new Vue({
el:'#root',
data:{
userInfo:{
account:'',
password:'',
age:18,
sex:'female',
hobby:[],
city:'beijing',
other:'',
agree:''
}
},
methods: {
printInfo(){
console.log(JSON.stringify(this.userInfo))
}
}
})
</script>
</html>頁(yè)面顯示如下:

vm顯示的data如下:

2. 日期格式化
- 在bootcn搜索dayjs,可以看到這個(gè)日期格式化的js文件
- 選擇復(fù)制鏈接,打開https://cdn.bootcdn.net/ajax/libs/dayjs/1.11.7/dayjs.min.js
- 右鍵另存為進(jìn)行保存
使用:
dayjs(1690497853551).format('YYYY年MM月DD日 HH:mm:ss')不傳時(shí)間戳,默認(rèn)就是當(dāng)前時(shí)間
3. 過(guò)濾器
不是必須要用的,用計(jì)算屬性、method方法也可以實(shí)現(xiàn)
- 場(chǎng)景:對(duì)要顯示的數(shù)據(jù)進(jìn)行特定格式化后再顯示(適用于一些簡(jiǎn)單邏輯的處理)
- 注冊(cè)過(guò)濾器:全局過(guò)濾器Vue.filter(name, callback)或局部過(guò)濾器new Vue{filters:{}}
- 使用過(guò)濾器:
{{xxx | 過(guò)濾器名}}或v-bind:屬性 = "xxx | 過(guò)濾器名" - 注意:
- 過(guò)濾器可以自動(dòng)接收管道符前的值,也可以接收額外參數(shù)、多個(gè)過(guò)濾器也可以串聯(lián)
- 并沒(méi)有改變?cè)镜臄?shù)據(jù), 是產(chǎn)生新的對(duì)應(yīng)的數(shù)據(jù)
使用示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javaScript" src="../js/vue.js"></script>
<script type="text/javaScript" src="../js/dayjs.min.js"></script>
</head>
<body>
<div id="root">
<h2>現(xiàn)在是:{{time | timeFormater('YYYY_MM_DD') | mySlice}}</h2>
</div>
<div id="root2">
<h2 :x="msg | mySlice">hello</h2>
</div>
<script type="text/javascript">
// 全局過(guò)濾器
Vue.filter('mySlice',function(value){
return value.slice(0,4)
})
new Vue({
el:'#root',
data:{
time:1690497853551
},
// 局部過(guò)濾器
filters:{
timeFormater(value,format='YYYY年MM月DD日 HH:mm:ss'){
return dayjs(value).format(format)
}
}
})
new Vue({
el:'#root2',
data:{
msg:'hello'
}
})
</script>
</body>
</html>頁(yè)面顯示效果:

到此這篇關(guān)于Vue使用v-model收集各種表單數(shù)據(jù)、過(guò)濾器的文章就介紹到這了,更多相關(guān)vue v-mode 表單數(shù)據(jù)、過(guò)濾器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue配置proxy代理接口報(bào)錯(cuò)2007 bad domain的解決
本文主要介紹了Vue配置proxy代理接口報(bào)錯(cuò)2007 bad domain的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
vue props default Array或是Object的正確寫法說(shuō)明
這篇文章主要介紹了vue props default Array或是Object的正確寫法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
antd vue v-decorator的取值與賦值問(wèn)題
這篇文章主要介紹了antd vue v-decorator的取值與賦值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
Vue如何獲取new Date().getTime()時(shí)間戳
在Web開發(fā)中,前端使用Vue.js獲取的是毫秒級(jí)時(shí)間戳,而PHP后端則是秒級(jí)時(shí)間戳,處理此類問(wèn)題時(shí),需要將PHP的時(shí)間戳乘以1000轉(zhuǎn)換為毫秒級(jí),以保證數(shù)據(jù)的一致性和正確的邏輯判斷2024-10-10
Vue中使用vue-plugin-hiprint插件進(jìn)行打印的功能實(shí)現(xiàn)
hiprint 是一個(gè)web 打印的js組件,無(wú)需安裝軟件,支持windows,macOS,linux 系統(tǒng),支持移動(dòng)端,PC端瀏覽器,angular,vue,react 等 分頁(yè)預(yù)覽,打印,操作簡(jiǎn)單,運(yùn)行快速,本文介紹了Vue中使用vue-plugin-hiprint插件進(jìn)行打印,需要的朋友可以參考下2025-04-04
詳解vue 路由跳轉(zhuǎn)四種方式 (帶參數(shù))
這篇文章主要介紹了vue 路由跳轉(zhuǎn)四種方式 (帶參數(shù)),本文通過(guò)實(shí)例代碼給大家介紹的詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04

