当前位置: 首页 > article >正文

Vue表单的整体处理

在前端的处理中,表单的处理永远是占高比例的。在BOM+DOM+js的时候是这样,在Vue的时候也是这样。Vue的表单处理做了特别的优化,如值绑定、数据验证、错误提示、修饰符等。

表单组件的示例:

<script setup lang="ts">
import {ref} from "vue"
import axios from "axios"

   const name=ref("");
   const gender=ref("");
   const birthDate=ref("");
   const phone=ref("");
   const applyReason=ref("");
   const scopes=ref([]);
   const luckyNumber=ref(1);

   function submitForm(){
      axios({
         method:"post",
         url:"",
         params:{
            name:name.value,
            gender:gender.value,
            birthDate:birthDate.value,
            phone:phone.value,
            applyReason:applyReason.value,
            scopes:scopes.value,
            lunckyNumber:luckyNumber.value
         }
      }).then((res)=>{
         console.log(res);
      });
   }

   function resetForm(){
       name.value="";
       gender.value="";
       birthDate.value="";
       phone.value="";
       applyReason.value="";
       scopes.value=[];
       luckyNumber.value=1;
   }
</script>

<template>
<form>
   <div>
      <div>
          姓名:
          <input v-model="name" id="name" placeholder="input your name"/>
      </div>
      <div>
          性别:
          <input type="radio" value="male" id="male" v-model="gender"/>
          <lable for="male">男</lable>
          <input type="radio" value="female" id="female" v-model="gender"/>
          <lable for="female">女</lable>
      </div>
      <div>
         出生日期:
         <input type="date" id="birthDate" v-model="birthDate"/>
      </div>
      <div>
         手机号码:
         <input type="text" v-model="phone"/>
      </div>
      <div>
          申请理由:
          <textarea v-model="applyReason" placeholder="add reason here"></textarea>
      </div>
      <div>
          涉猎范围:
          <input type="checkbox" id="reading" value="reading" v-model="scopes"/>
          <lable for="reading">阅读</lable>
          <input type="checkbox" id="sports" value="sports" v-model="scopes"/>
          <lable for="sports">运行</lable>
          <input type="checkbox" id="dreaming" value="dreaming" v-model="scopes"/>
          <lable for="dreaming">冥想</lable>
          <input type="checkbox" id="playing" value="playing" v-model="scopes"/>
          <lable for="playing">操作</lable>
      </div>
      <div>
         幸运数字:
         <select name="select" v-model="luckyNumber">
               <option selected>1</option>
               <option>2</option>
               <option>3</option>
               <option>4</option>
               <option>5</option>
               <option>6</option>
               <option>7</option>
               <option>8</option>
               <option>9</option>
         </select>
      </div>
      <div>
            <button type="button" @click="submitForm">提交</button>
            <button type="button" @click="resetForm">重置</button>
      </div>
   </div>    
</form>
</template>

<style>

</style>

v-model在Vue中有三种修饰符:

v-model.lazy :由原来的input事件之后更新v-model的值改为change事件之后更新v-model的值,这对复合组件和计算属性有很大的作用

v-model.number  :用户输入的内容会被自动转换为数字,输入框有type="number"时会自动启用

v-model.trim  :用户的输入内容会自动去除两端的空格


http://www.kler.cn/news/148388.html

相关文章:

  • 成为AI产品经理——模型评估概述
  • GeoTrust证书
  • 96.STL-遍历算法 transform
  • 文章解读与仿真程序复现思路——电力自动化设备EI\CSCD\北大核心《考虑碳排放分摊的综合能源服务商交易策略》
  • HttpRunner原来还能这么用,大开眼界!!!
  • WPF创建进度条
  • 「计算机网络」Cisco Packet Tracker计算机网络仿真器的使用
  • YOLOv5算法进阶改进(5)— 主干网络中引入SCConv | 即插即用的空间和通道维度重构卷积
  • android项目之调用webview
  • TypeScript学习记录
  • LeetCode51. N-Queens
  • java后端实现登录退出功能,并用过滤器验证
  • android trace文件的抓取与查看方法
  • 【Lidar】基于Python的点云数据下采样+体素显示
  • tauri中使用rust调用动态链接库例子(使用libloading库和libc库)
  • ubuntu22.04 arrch64版在线安装java环境
  • C语言-指针讲解(3)
  • 用通俗的方式讲解Transformer:从Word2Vec、Seq2Seq逐步理解到GPT、BERT
  • 人机交互3——多主题多轮对话
  • TOD和PPS精确时间同步技术
  • C#面向对象
  • 2023网络安全产业图谱
  • 02-Java集合之双列集合,如HashMap,Hashtable,Properties,TreeMap的底层结构
  • 人工智能技术发展漫谈
  • 【Linux】信号
  • 《2023全球隐私计算报告》正式发布!
  • C语言错误处理之“非局部跳转<setjmp.h>头文件”
  • python 爬虫之 爬取网站信息并保存到文件
  • C++初阶--String类的使用
  • TCP 传输可靠性问题