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>
相关推荐
呱牛do it7 小时前
企业级门户网站设计与实现:基于SpringBoot + Vue3的全栈解决方案(Day 7)
java·vue
呱牛do it3 天前
企业级门户网站设计与实现:基于SpringBoot + Vue3的全栈解决方案(Day 5)
java·vue
无心使然云中漫步3 天前
Openlayers调用ArcGis地图服务之一 —— 地图切片(/tile)
前端·arcgis·vue·数据可视化
Python私教3 天前
我在开发 ShadcnVueAdmin 时发现了一个 Claude Code 超级插件
vue
无心使然云中漫步4 天前
Openlayers调用ArcGis地图服务之三 —— 要素查询(/query)
前端·arcgis·vue·数据可视化
呱牛do it4 天前
企业级门户网站设计与实现:基于SpringBoot + Vue3的全栈解决方案(Day 3)
java·vue
呱牛do it4 天前
企业级门户网站设计与实现:基于SpringBoot + Vue3的全栈解决方案(Day 4)
java·vue
无心使然云中漫步5 天前
Openlayers调用ArcGis地图服务之二 —— 动态地图(/export)
前端·arcgis·vue·数据可视化
神探小白牙5 天前
3D饼图,带背景图和自定义图例(threejs)
开发语言·前端·javascript·3d·vue
是吗乔治5 天前
vuetify实现excel表格粘贴效果
前端·vue.js·vue·excel