Vue练习 v-model 指令在状态和表单输入之间创建双向绑定

效果:

html 复制代码
<template>
  <h2>Text Input</h2>
  <input v-model="text"> {{ text }}

  <h2>Checkbox</h2>
  <input type="checkbox" id="checkbox" v-model="checked">
  <label for="checkbox">Checked: {{ checked }}</label>

	<h2>Sports Checkbox</h2>
	<input type="checkbox" id="sing" value="唱" v-model="Sports">
	<label for="sing">唱</label>
	<input type="checkbox" id="jump" value="跳" v-model="Sports">
	<label for="jump">跳</label>
	<input type="checkbox" id="rap" value="rap" v-model="Sports">
	<label for="rap">rap</label>
	<input type="checkbox" id="basketball" value="篮球" v-model="Sports">
	<label for="basketball">篮球</label>
	<p>Sports :<pre>{{Sports}}</pre></p>

  <h2>Radio</h2>
  <input type="radio" id="boy" value="男" v-model="sex">
  <label for="boy">男</label>
  <input type="radio" id="girl" value="女" v-model="sex">
  <label for="girl">女</label>
	<input type="radio" id="unknown" value="未知" v-model="sex">
	<label for="unknown">未知</label>
  <br>
  <p><span>Sex : {{ sex }}</span></p>

  <h2>Select one</h2>
  <select v-model="selected">
    <option disabled value="">Please select one</option>
    <option>A</option>
    <option>B</option>
    <option>C</option>
  </select>
  <span>Selected : {{ selected }}</span>

  <h2>Multi Select</h2>
  <select v-model="multiSelected" multiple style="width:100px">
    <option>A</option>
    <option>B</option>
    <option>C</option>
  </select>
  <span>Selected : {{ multiSelected }}</span>
</template>

<script>
export default {
  data() {
    return {
      text: 'Please enter text',
      checked: true,
      Sports:['唱'],
      sex: '男',
      selected: 'B',
      multiSelected: ['A']
    }
  }
}
</script>

<style>
  label{
    margin-right:8px;
    color:rgb(25, 159, 221)
  }
  select{
    display: block;
    margin-bottom: 10px;
  }

</style>
相关推荐
cgsthtm1 小时前
RuoYi.Net后端返回雪花ID前端精度丢失问题
oracle·vue·精度丢失·雪花id·ruoyi.net
玩代码4 小时前
使用 nvm(Node Version Manager) 高效管理Node.js
node.js·vue·nvm
bdawn16 小时前
Vue3 项目首屏加载性能优化全攻略
性能优化·vue·策略·分包
Orange_sparkle1 天前
若依使用基本步骤
java·vue
肖祥1 天前
uni-app x封装request,统一API接口请求
vue
阿喵派我来抓鱼1 天前
深入理解 AI 流式接口:从请求到响应的完整解析
react.js·ai·前端框架·vue
Ciito2 天前
查看Vue项目当前的Vue CLI版本号
vue
肖祥2 天前
uni-app x使用uview-plus
vue
dcloud_jibinbin2 天前
【uniapp】体验优化:开源工具集 uni-toolkit 发布
前端·webpack·性能优化·小程序·uni-app·vue
知识分享小能手2 天前
微信小程序入门学习教程,从入门到精通,电影之家小程序项目知识点详解 (17)
前端·javascript·学习·微信小程序·小程序·前端框架·vue