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>
相关推荐
biyezuopinvip13 小时前
基于Spring Boot的企业网盘的设计与实现(任务书)
java·spring boot·后端·vue·ssm·任务书·企业网盘的设计与实现
biyezuopinvip19 小时前
基于Spring Boot的企业网盘的设计与实现(毕业论文)
java·spring boot·vue·毕业设计·论文·毕业论文·企业网盘的设计与实现
Cult Of3 天前
Alicea Wind的个人网站开发日志(2)
开发语言·python·vue
Byron07074 天前
从多端割裂到体验统一:基于 Vue 生态的跨端架构落地实战
vue·多端
计算机程序设计小李同学4 天前
基于 Spring Boot + Vue 的龙虾专营店管理系统的设计与实现
java·spring boot·后端·spring·vue
沐墨染4 天前
Vue实战:自动化研判报告组件的设计与实现
前端·javascript·信息可视化·数据分析·自动化·vue
奔跑的呱呱牛4 天前
viewer-utils 图片预览工具库
javascript·vue·react
Cult Of4 天前
Alicea Wind的个人网站开发日志(1)
python·vue
Polaris_YJH4 天前
使用Vue3+Vite+Pinia+elementUI搭建初级企业级项目
前端·javascript·elementui·vue
Mr Xu_4 天前
【Vue3 + ECharts 实战】正确使用 showLoading、resize 与 dispose 避免内存泄漏
前端·信息可视化·vue·echarts