您好,登錄后才能下訂單哦!
這篇文章主要介紹“vue中如何利用v-model綁定表單控件”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“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>
輸出結果:
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>
輸出結果:
3、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="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>
輸出結果:選中為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>
輸出結果:
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>
輸出結果:
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>
輸出結果:
多選列表
<!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>
輸出結果:
6、動態選項,用 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>
輸出結果:
關于“vue中如何利用v-model綁定表單控件”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。