您好,登錄后才能下訂單哦!
這篇文章主要介紹“vue中如何利用v-model綁定表單控件”的相關(guān)知識(shí),小編通過實(shí)際案例向大家展示操作過程,操作方法簡單快捷,實(shí)用性強(qiáng),希望這篇“vue中如何利用v-model綁定表單控件”文章能幫助大家解決問題。
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)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。