Vue——表单输入绑定

1、代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>08_表单输入绑定</title>
</head>
<body>
<!--
1. 使用v-model(双向数据绑定)自动收集数据
  text/textarea
  checkbox
  radio
  select
-->
<div id="demo">
  <form action="/xxx" @submit.prevent="handleSubmit">
      <span>用户名:</span>
      <input type="text" v-model="username"><br>

      <span>密码:</span>
      <input type="password" v-model="pwd"><br>

      <span>性别</span>
      <input type="radio" id="female" value="女" v-model="sex">
      <label for="female"></label>
      <input type="radio" id="male" value="男" v-model="sex">
      <label for="male"></label><br>

      <span>爱好:</span>
      <input type="checkbox" id="basket" value="basket" v-model="likes">
      <span for="basket">篮球</span>
      <input type="checkbox" id="foot" value="foot"  v-model="likes">
      <span v-for="foot">足球</span>
      <input type="checkbox" id="pingpang" value="pingpang"  v-model="likes">
      <span v-for="pingpang">乒乓球</span><br>

      <span>城市:</span>
      <select v-model="cityId">
          <option value="">未选择</option>
          <option  :value="city.id" v-for="(city,index) in allCitys" :key="index">{{city.name}}</option>
      </select><br>

      <span>介绍:</span>
      <textarea rows="10" v-model="desc"></textarea><br><br>

      <input type="submit" value="注册">
  </form>
</div>

<script type="text/javascript" src="../js/vue.js"></script>
<script type="text/javascript">
  new Vue({
      el:"#demo",
      data:{
        username:'',
        pwd:'',
        sex:'女',/*默认选中女*/
        likes:[],
        allCitys:[{id:'1',name:'北京'},{id:'2',name:'上海'},{id:'3',name:'深圳'}],
        cityId:'',
        desc:''
      },
      methods:{
          handleSubmit(){
              console.log(this.username+"-----"+this.pwd)
          }
      }
  })
</script>
</body>
</html>

2、效果图如下:
Vue——表单输入绑定_第1张图片

你可能感兴趣的:(vue)