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>
相关推荐
wanzhong23331 天前
开发日记13-响应式变量
开发语言·前端·javascript·vue
wanzhong23331 天前
开发日记14-vite配置多环境
服务器·前端·vue
刘一说1 天前
Vue3 组合式 API(Composition API):逻辑复用的革命性实践
vue.js·vue
天天打码2 天前
Svelte-无虚拟DOM、极致性能的现代高性能Web开发框架!
前端·node.js·vue·svelte
LXMXHJ2 天前
项目之html+javaScript
java·vue
期货资管源码2 天前
智星期货资管子账户软件pc端开发技术栈的选择
c语言·数据结构·c++·vue
@二十六2 天前
表格行拖拽排序
vue·react·表格拖拽
jay神3 天前
基于Java的水果网上订购平台
java·mysql·vue·springboot·计算机毕业设计
午安~婉4 天前
整理知识点
前端·javascript·vue
Shi_haoliu4 天前
SolidTime 在 Rocky Linux 9.5 上的完整部署流程
linux·运维·nginx·postgresql·vue·php·laravel