zoukankan      html  css  js  c++  java
  • 怎样在 Vue 中使用 v-model 处理表单?

    主要是通过 v-model 对表单元素做数据的 双向绑定. 用法其实也很简单, 只是因为表单元素有不同类型, 处理方式有些许不同, 这点需要注意. 

    1. 如果是 输入框 , 可以直接使用 v-model="" , 注意这里的 .trim | .number | .lazy 是三个 v-model修饰符. 表示 去除输入内容的收尾空格 | 将输入的字符串转换为数值类型 | 让数据的更新在输入改变时进行.

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
      <title>Vue Test</title>
      <style>
          .style1 {
              width: 100px; height: 100px; background-color: tomato;
              text-align: center; line-height: 100px; color: white;
              cursor: pointer;
          }
      </style>
    </head>
    <body>
        <div id="app">
            <form action="">
                <input type="text" v-model.number.trim.lazy="text" />
                <p>{{ text }}</p>
            </form>
        </div>
        <script>
            var vApp = new Vue({
                el: "#app",
                data: {
                    text: ""
                }
            })
        </script>
    </body>
    </html>

    2. 表单中的 多选框复选框 中的 v-model 绑定的是 name 属性, 只是数据类型不一样, 多选框 需要用 数组 来装

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
      <title>Vue Test</title>
      <style>
          .style1 {
              width: 100px; height: 100px; background-color: tomato;
              text-align: center; line-height: 100px; color: white;
              cursor: pointer;
          }
      </style>
    </head>
    <body>
        <div id="app">
            <form action="">
                <input id="radio1" type="radio" v-model="radioName" value="radioTest1" />
                <label for="radio1">单选示例1</label>
                <input id="radio2" type="radio" v-model="radioName" value="radioTest2" />
                <label for="radio2">单选示例2</label>
                <p>单选选中状态: {{ radioName }}</p>
    
                <input id="checkbox1" type="checkbox" v-model="checkboxName" value="checkboxTest1" />
                <label for="checkbox1">多选示例1</label>
                <input id="checkbox2" type="checkbox" v-model="checkboxName" value="checkboxTest2" />
                <label for="checkbox2">多选示例2</label>
                <input id="checkbox3" type="checkbox" v-model="checkboxName" value="checkboxTest3" />
                <label for="checkbox3">多选示例3</label>
                <p>多选选中状态: {{ checkboxName }}</p>
            </form>
        </div>
        <script>
            var vApp = new Vue({
                el: "#app",
                data: {
                    radioName: false,
                    checkboxName: []
                }
            })
        </script>
    </body>
    </html>

    3. 下面是 下拉列表双向数据绑定

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
      <title>Vue Test</title>
      <style>
          .style1 {
              width: 100px; height: 100px; background-color: tomato;
              text-align: center; line-height: 100px; color: white;
              cursor: pointer;
          }
      </style>
    </head>
    <body>
        <div id="app">
            <form action="">
                <select v-model="selectTest" name="test">
                    <option value="">请选择</option>
                    <option value="water">水水水</option>
                    <option value="fire">火火火</option>
                </select>
                <p>选择: {{ selectTest }}</p>
            </form>
        </div>
        <script>
            var vApp = new Vue({
                el: "#app",
                data: {
                    selectTest: ""
                }
            })
        </script>
    </body>
    </html>

  • 相关阅读:
    HAProxy、Keepalived 在 Ocatvia 的应用实现与分析
    Octavia 的 HTTPS 与自建、签发 CA 证书
    Octavia 创建 loadbalancer 的实现与分析
    OpenStack Rally 质量评估与自动化测试利器
    自建 CA 中心并签发 CA 证书
    Failed building wheel for netifaces
    通过 vSphere WS API 获取 vCenter Datastore Provisioned Space 置备空间
    OpenStack Placement Project
    我们建了一个 Golang 硬核技术交流群(内含视频福利)
    没有图形界面的软件有什么用?
  • 原文地址:https://www.cnblogs.com/aisowe/p/11434231.html
Copyright © 2011-2022 走看看