Home >Web Front-end >JS Tutorial >Example tutorial of vue v-model form control binding

Example tutorial of vue v-model form control binding

Y2J
Y2JOriginal
2017-05-24 11:08:061934browse

This article mainly introduces the relevant information of vue v-model form control binding in detail, which has certain reference value. Interested friends can refer to the

v-model command in Create two-way data binding on form control elements. Examples are explained below.

1. v-model two-way binding text

<!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>

Output result:

2. v- model bidirectionally binds multiple lines of text, similar to the example above.

<!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>

Output result:

3. v-model binding Check box

<!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>

Output result: true if selected, false if not selected


##

<!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>

Output result:

4. v-model bindingRadio button

<!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>

Output result:

5. v-model bindingDrop-down list

<!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>

Output result:

Multi-select list


<!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>

Output result:

6. Dynamic options, use v-for Rendering:

<!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>

Output result:

[Related recommendations]


1.

Javascript Free Video tutorial

2.

Code example of JS implementation of multi-level menu bar

3.

Detailed examples of commonly used middleware body-parser in Nodejs

4.

JavaScript form verification implementation code_javascript skills

5.

Single line of JS to implement mobile money format verification

The above is the detailed content of Example tutorial of vue v-model form control binding. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn