Vue 表單控件綁定的實(shí)現(xiàn)示例
本文介紹了Vue 表單控件綁定的實(shí)現(xiàn)示例,感覺這個(gè)地方知識點(diǎn)挺多的,而且很重要,所以,今天添加一點(diǎn)小筆記。
基礎(chǔ)用法
可以用 v-model 指令在表單控件元素上創(chuàng)建雙向數(shù)據(jù)綁定。根據(jù)控件類型它自動選取正確的方法更新元素。盡管有點(diǎn)神奇,v-model 不過是語法糖,在用戶輸入事件中更新數(shù)據(jù),以及特別處理一些極端例子。
Text
<span>Message is: {{ message }}</span> <br> <input type="text" v-model="message" placeholder="edit me">
Checkbox
單個(gè)勾選框,邏輯值:
<input type="checkbox" id="checkbox" v-model="checked"> <label for="checkbox">{{ checked }}</label>
多個(gè)勾選框,綁定到同一個(gè)數(shù)組:
<input type="checkbox" id="jack" value="Jack" v-model="checkedNames"> <label for="jack">Jack</label> <input type="checkbox" id="john" value="John" v-model="checkedNames"> <label for="john">John</label> <input type="checkbox" id="mike" value="Mike" v-model="checkedNames"> <label for="mike">Mike</label> <br> <span>Checked names: {{ checkedNames | json }}</span>
new Vue({ el: '...', data: { checkedNames: [] } })
Radio
<input type="radio" id="one" value="One" v-model="picked"> <label for="one">One</label> <br> <input type="radio" id="two" value="Two" v-model="picked"> <label for="two">Two</label> <br> <span>Picked: {{ picked }}</span>
Select
單選:
<select v-model="selected"> <option selected>A</option> <option>B</option> <option>C</option> </select> <span>Selected: {{ selected }}</span>
多選(綁定到一個(gè)數(shù)組):
<select v-model="selected" multiple> <option selected>A</option> <option>B</option> <option>C</option> </select> <br> <span>Selected: {{ selected | json }}</span>
動態(tài)選項(xiàng),用 v-for 渲染:
<select v-model="selected"> <option v-for="option in options" v-bind:value="option.value"> {{ option.text }} </option> </select> <span>Selected: {{ selected }}</span> new Vue({ el: '...', data: { selected: 'A', options: [ { text: 'One', value: 'A' }, { text: 'Two', value: 'B' }, { text: 'Three', value: 'C' } ] } })
值綁定
對于單選按鈕,勾選框及選擇框選項(xiàng),v-model 綁定的值通常是靜態(tài)字符串(對于勾選框是邏輯值):
<!-- 當(dāng)選中時(shí),`picked` 為字符串 "a" --> <input type="radio" v-model="picked" value="a"> <!-- `toggle` 為 true 或 false --> <input type="checkbox" v-model="toggle"> <!-- 當(dāng)選中時(shí),`selected` 為字符串 "abc" --> <select v-model="selected"> <option value="abc">ABC</option> </select>
但是有時(shí)我們想綁定值到 Vue 實(shí)例一個(gè)動態(tài)屬性上??梢杂?v-bind 做到。 而且 v-bind允許綁定輸入框的值到非字符串值。
Checkbox
<input type="checkbox" v-model="toggle" v-bind:true-value="a" v-bind:false-value="b"> // 選中 vm.toggle === vm.a // 取消選中 vm.toggle === vm.b
Radio
<input type="radio" v-model="pick" v-bind:value="a"> // 選中 vm.pick === vm.a
Select Options
<select v-model="selected"> <!-- 對象字面量 --> <option v-bind:value="{ number: 123 }">123</option> </select> // 選中 typeof vm.selected // -> 'object' vm.selected.number // -> 123
參數(shù)特性
lazy
在默認(rèn)情況下,v-model 在input 事件中同步輸入框值與數(shù)據(jù),可以添加一個(gè)特性lazy,從而改到在 change 事件中同步:
<!-- 在 "change" 而不是 "input" 事件中更新 --> <input v-model="msg" lazy>
number
如果想自動將用戶的輸入保持為數(shù)字,可以添加一個(gè)特性 number:
<input v-model="age" number>
debounce
debounce 設(shè)置一個(gè)最小的延時(shí),在每次敲擊之后延時(shí)同步輸入框的值與數(shù)據(jù)。如果每次更新都要進(jìn)行高耗操作(例如在輸入提示中 Ajax 請求),它較為有用。
<input v-model="msg" debounce="500">
注意 debounce 參數(shù)不會延遲 input 事件:它延遲“寫入”底層數(shù)據(jù)。因此在使用 debounce時(shí)應(yīng)當(dāng)用 vm.$watch() 響應(yīng)數(shù)據(jù)的變化。若想延遲 DOM 事件,應(yīng)當(dāng)使用 debounce 過濾器。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot運(yùn)用vue+echarts前后端交互實(shí)現(xiàn)動態(tài)圓環(huán)圖
我們做項(xiàng)目的時(shí)候,常常需要一些統(tǒng)計(jì)圖來展示我們的數(shù)據(jù),作為web開發(fā)人員,會實(shí)現(xiàn)統(tǒng)計(jì)圖是我們必會的技能。我將帶大家來實(shí)現(xiàn)動態(tài)餅圖的實(shí)現(xiàn),感興趣的可以了解一下2021-06-06基于vue實(shí)現(xiàn)一個(gè)禪道主頁拖拽效果
最近在做一個(gè)基于vue的后臺管理項(xiàng)目。接下來通過本文給大家分析一款基于vue做一個(gè)禪道主頁拖拽效果,需要的朋友可以參考下2019-05-05vue+element-ui表格自定義列模版的實(shí)現(xiàn)
本文主要介紹了vue+element-ui表格自定義列模版的實(shí)現(xiàn),通過插槽完美解決了element-ui表格自定義列模版的問題,具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05在vue中created、mounted等方法使用小結(jié)
這篇文章主要介紹了在vue中created、mounted等方法使用小結(jié),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07