vue v-model表单控件绑定的实例教程

Y2J
Lepaskan: 2017-05-24 11:08:06
asal
1894 orang telah melayarinya

这篇文章主要为大家详细介绍了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>
Salin selepas log masuk

输出结果:

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>
Salin selepas log masuk

输出结果:

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>
Salin selepas log masuk

输出结果:选中为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>
Salin selepas log masuk

输出结果:

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>
Salin selepas log masuk

输出结果:

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>
Salin selepas log masuk

输出结果:

多选列表


<!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>
Salin selepas log masuk

输出结果:

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>
Salin selepas log masuk

输出结果:

【相关推荐】

1. Javacript免费视频教程

2. JS实现多级菜单栏的代码示例

3. Nodejs中常用中间件body-parser的实例详解

4. JavaScript表单验证实现代码_javascript技巧

5. 单行 JS 实现移动端金钱格式检验

Atas ialah kandungan terperinci vue v-model表单控件绑定的实例教程. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan