欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

vue v-model表单控件绑定详解

程序员文章站 2022-03-07 08:54:53
v-model 指令在表单控件元素上创建双向数据绑定,下面一一进行示例解释。 1、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>
<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>

输出结果:

vue v-model表单控件绑定详解

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 style="white-space: pre">{{ 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>

输出结果:

vue v-model表单控件绑定详解

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

vue 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="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>

输出结果:

vue v-model表单控件绑定详解

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>

输出结果:

vue v-model表单控件绑定详解

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>

输出结果:

vue 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" multiple style="width: 50px">
  <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>

输出结果:

vue v-model表单控件绑定详解

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表单控件绑定详解

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。