您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)vue 中怎么利用v-model綁定表單控件,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
1、v-model 雙向綁定文本
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="vue.js"></script> </head> <body> <div id="app"> <input v-model="message" placeholder="edit me"> <p>Message is: {{ message }}</p> </div> </body> <script> var vm = new Vue({ el:"#app", data: { message: '綁定文本' } }) </script> </html>
輸出結(jié)果:
2、v-model 雙向綁定多行文本,與上面的例子相似。
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="vue.js"></script> </head> <body> <div id="app"> <<span>Multiline message is:</span> <p >{{ message }}</p> <br> <textarea v-model="message" placeholder="add multiple lines"></textarea> </div> </body> <script> var vm = new Vue({ el:"#app", data: { message: '綁定多行文本' } }) </script> </html>
輸出結(jié)果:
3、v-model 綁定復(fù)選框
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="vue.js"></script> </head> <body> <div id="app"> <input type="checkbox" id="checkbox" v-model="checked"> <label for="checkbox">{{ checked }}</label> </div> </body> <script> var vm = new Vue({ el:"#app", data: { checked: 'true' } }) </script> </html>
輸出結(jié)果:選中為true 不選中則為false
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="vue.js"></script> </head> <body> <div id="app"> <input type="checkbox" id="jack" value="劉二狗" v-model="checkedNames"> <label for="jack">Jack</label> <input type="checkbox" id="john" value="張麻子" v-model="checkedNames"> <label for="john">John</label> <input type="checkbox" id="mike" value="小狗子" v-model="checkedNames"> <label for="mike">Mike</label> <br> <span>Checked names: {{ checkedNames }}</span> </div> </body> <script> var vm = new Vue({ el:"#app", data: { checkedNames: [] } }) </script> </html>
輸出結(jié)果:
4、v-model 綁定單選按鈕
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="vue.js"></script> </head> <body> <div id="app"> <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> </div> </body> <script> var vm = new Vue({ el:"#app", data: { picked: '' } }) </script> </html>
輸出結(jié)果:
5、v-model 綁定下拉列表
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="vue.js"></script> </head> <body> <div id="app"> <select v-model="selected"> <option>A</option> <option>B</option> <option>C</option> </select> <span>Selected: {{ selected }}</span> </div> </body> <script> var vm = new Vue({ el:"#app", data: { selected: '' } }) </script> </html>
輸出結(jié)果:
多選列表
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="vue.js"></script> </head> <body> <div id="app"> <select v-model="selected" multiple > <option>A</option> <option>B</option> <option>C</option> </select> <br> <span>Selected: {{ selected }}</span> </div> </body> <script> var vm = new Vue({ el:"#app", data: { selected: [] } }) </script> </html>
輸出結(jié)果:
6、動(dòng)態(tài)選項(xiàng),用 v-for 渲染:
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="vue.js"></script> </head> <body> <div id="app"> <select v-model="selected"> <option v-for="option in options" v-bind:value="option.value"> {{ option.text }} </option> </select> <span>Selected: {{ selected }}</span> </div> </body> <script> var vm = new Vue({ el:"#app", data: { selected: 'A', options: [ { text: 'One', value: 'A' }, { text: 'Two', value: 'B' }, { text: 'Three', value: 'C' } ] } }) </script> </html>
輸出結(jié)果:
關(guān)于vue 中怎么利用v-model綁定表單控件就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。