首頁  >  文章  >  web前端  >  vue v-model表單控制項綁定的實例教程

vue v-model表單控制項綁定的實例教程

Y2J
Y2J原創
2017-05-24 11:08:061868瀏覽

這篇文章主要為大家詳細介紹了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: &#39;绑定文本&#39;
  }
 })
</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: &#39;绑定多行文本&#39;
  }
 })
</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: &#39;true&#39;
  }
 })
</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: &#39;&#39;
  }
 })
</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>
<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: &#39;&#39;
  }
 })
</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>

輸出結果:

#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>
<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: &#39;A&#39;,
   options: [
    { text: &#39;One&#39;, value: &#39;A&#39; },
    { text: &#39;Two&#39;, value: &#39;B&#39; },
    { text: &#39;Three&#39;, value: &#39;C&#39; }
   ]
  }
 })
</script>
</html>

輸出結果:

#【相關建議】

1. 

Javacript免費影片教學

2. ###JS實作多層級選單列的程式碼範例#########3.### Nodejs中常用中間件body-parser的實例詳解#########4. ###JavaScript表單驗證實作程式碼_javascript技巧#########5. ###單行JS 實作行動裝置金錢格式檢定#######

以上是vue v-model表單控制項綁定的實例教程的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn