• 513 v-model:表单绑定,原理,radio,checkbox,select,修饰符,值绑定


    1.表单绑定v-model

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <title>Title</title>
    </head>
    
    <body>
    
      <div id="app">
        <input type="text" v-model="message">
        {{message}}
      </div>
    
      <script src="../js/vue.js"></script>
      <script>
        const app = new Vue({
          el: '#app',
          data: {
            message: '你好啊'
          }
        })
      </script>
    
    </body>
    
    </html>
    

    2.v-model原理

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>Title</title>
    </head>
    <body>
    
    <div id="app">
      <!--<input type="text" v-model="message">-->
      <!--<input type="text" :value="message" @input="valueChange">-->
      <input type="text" :value="message" @input="message = $event.target.value">
      <!-- 也可以用change事件代替input事件 -->
      <input type="text" :value="msg" @change="msg = $event.target.value">
      <h2>{{message}}</h2>
    </div>
    
    <script src="../js/vue.js"></script>
    <script>
      const app = new Vue({
        el: '#app',
        data: {
          message: '你好啊'
        },
        methods: {
          valueChange(event) {
            this.message = event.target.value;
          }
        }
      })
    </script>
    
    </body>
    </html>
    

    3.v-model:radio

    存在多个单选框时,v-model结合radio类型

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <title>Title</title>
    </head>
    
    <body>
    
      <div id="app">
        <label for="male">
          <!-- input都有value属性,保存input值,这里加上value的目的是双向数据绑定,获取用户选择的信息 -->
          <!-- 把选中的单选框的value值双向绑定给了sex,得有value -->
          <input type="radio" id="male" value="男" v-model="sex">男
        </label>
        <label for="female">
          <input type="radio" id="female" value="女" v-model="sex">女
        </label>
        <h2>您选择的性别是: {{sex}}</h2>
      </div>
    
      <script src="../js/vue.js"></script>
      <script>
        const app = new Vue({
          el: '#app',
          data: {
            message: '你好啊',
            sex: '男'
          }
        })
      </script>
    
    </body>
    
    </html>
    

    4.v-model:checkbox

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>Title</title>
    </head>
    <body>
    
    <div id="app">
      <!--1.checkbox单选框-->
      <!--<label for="agree">-->
        <!--<input type="checkbox" id="agree" v-model="isAgree">同意协议-->
      <!--</label>-->
      <!--<h2>您选择的是: {{isAgree}}</h2>-->
      <!--<button :disabled="!isAgree">下一步</button>-->
    
      <!--2.checkbox多选框-->
      <!-- 把选中的复选框的value值双向绑定给了hobbies,得有value --> 
      <input type="checkbox" value="篮球" v-model="hobbies">篮球
      <input type="checkbox" value="足球" v-model="hobbies">足球
      <input type="checkbox" value="乒乓球" v-model="hobbies">乒乓球
      <input type="checkbox" value="羽毛球" v-model="hobbies">羽毛球
      <h2>您的爱好是: {{hobbies}}</h2>
    
      <label v-for="item in originHobbies" :for="item">
        <input type="checkbox" :value="item" :id="item" v-model="hobbies">{{item}}
      </label>
    </div>
    
    <script src="../js/vue.js"></script>
    <script>
      const app = new Vue({
        el: '#app',
        data: {
          message: '你好啊',
          isAgree: false, // 单选框
          hobbies: [], // 多选框,
          originHobbies: ['篮球', '足球', '乒乓球', '羽毛球', '台球', '高尔夫球']
        }
      })
    </script>
    
    </body>
    </html>
    
    

    5.v-model:select

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <title>Title</title>
    </head>
    
    <body>
    
      <div id="app">
        <!--1.选择一个,v-model加给select,而不是option-->
        <select name="abc" v-model="fruit">
          <option value="苹果">苹果</option>
          <option value="香蕉">香蕉</option>
          <option value="榴莲">榴莲</option>
          <option value="葡萄">葡萄</option>
        </select>
        <h2>您选择的水果是: {{fruit}}</h2>
    
        <!--2.选择多个,加上属性multiple-->
        <select name="abc" v-model="fruits" multiple>
          <option value="苹果">苹果</option>
          <option value="香蕉">香蕉</option>
          <option value="榴莲">榴莲</option>
          <option value="葡萄">葡萄</option>
        </select>
        <h2>您选择的水果是: {{fruits}}</h2>
      </div>
    
      <script src="../js/vue.js"></script>
      <script>
        const app = new Vue({
          el: '#app',
          data: {
            message: '你好啊',
            fruit: '香蕉',
            fruits: []
          }
        })
      </script>
    
    </body>
    
    </html>
    

    6.修饰符

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <title>Title</title>
    </head>
    
    <body>
    
      <div id="app">
        <!--1.修饰符: lazy-->
        <input type="text" v-model.lazy="message">
        <h2>{{message}}</h2>
    
    
        <!--2.修饰符: number 【只要是数字开头,v-model.number的值类型都是number,否则是string】-->
        <input type="number" v-model.number="age">
        <h2>{{age}}-{{typeof age}}</h2>
    
        <!--3.修饰符: trim-->
        <input type="text" v-model.trim="name">
        <h2>您输入的名字:{{name}}</h2>
      </div>
    
      <script src="../js/vue.js"></script>
      <script>
        const app = new Vue({
          el: '#app',
          data: {
            message: '你好啊',
            age: 0,
            name: ''
          }
        })
    
        var age = 0
        age = '1111'
        age = '222'
      </script>
    
    </body>
    
    </html>
    

    7.值绑定

    <!-- v-model中的值绑定 -->
    <label v-for="item in originHobbies" :for="item">
    <input type="checkbox" :value="item" :id="item" v-model="selectHobbies">{{item}}
    </label>
    <h3>你的选择是:{{selectHobbies}}</h3>
    
  • 相关阅读:
    十三、Sleuth分布式请求链路追踪
    十二、SpringCloud Stream消息驱动
    十一、SpringCloud Bus 消息总线
    Linux命令(权限管理)
    Linux命令(文件管理)
    Linux的文件和目录
    Linux介绍及安装
    Docker
    Nginx
    13、SpringBoot整合Mybatis-Plus
  • 原文地址:https://www.cnblogs.com/jianjie/p/13535639.html
Copyright © 2020-2023  润新知