這篇文章主要為大家詳細介紹了vue v-model表單控制項綁定的相關資料,具有一定的參考價值,有興趣的小夥伴們可以參考一下
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> <p id="app"> <input v-model="message" placeholder="edit me"> <p>Message is: {{ message }}</p> </p> </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> <p id="app"> <<span>Multiline message is:</span> <p style="white-space: pre">{{ message }}</p> <br> <textarea v-model="message" placeholder="add multiple lines"></textarea> </p> </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> <p id="app"> <input type="checkbox" id="checkbox" v-model="checked"> <label for="checkbox">{{ checked }}</label> </p> </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> <p 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> </p> </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> <p 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> </p> </body> <script> var vm = new Vue({ el:"#app", data: { picked: '' } }) </script> </html>
輸出結果:
下拉清單
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="vue.js"></script> </head> <body> <p id="app"> <select v-model="selected"> <option>A</option> <option>B</option> <option>C</option> </select> <span>Selected: {{ selected }}</span> </p> </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> <p id="app"> <select v-model="selected" multiple style="width: 50px"> <option>A</option> <option>B</option> <option>C</option> </select> <br> <span>Selected: {{ selected }}</span> </p> </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> <p 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> </p> </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>輸出結果: 1. 2. ###JS實作多層級選單列的程式碼範例#########3.### Nodejs中常用中間件body-parser的實例詳解#########4. ###JavaScript表單驗證實作程式碼_javascript技巧#########5. ###單行JS 實作行動裝置金錢格式檢定#######
以上是vue v-model表單控制項綁定的實例教程的詳細內容。更多資訊請關注PHP中文網其他相關文章!