vue实现表单录入小案例

作者:小羽向前跑 时间:2024-05-09 15:11:07 

本文实例为大家分享了vue实现表单录入的具体代码,供大家参考,具体内容如下

最终效果:

vue实现表单录入小案例

代码:


<template>
<div id="app">
<!--第一部分-->
<fieldset>
 <legend>学生录入系统</legend>
 <div>
 <span>姓名:</span>
 <input type="text" placeholder="请输入姓名" v-model="newStudent.name">
 </div>
 <div>
 <span>年龄:</span>
 <input type="text" placeholder="请输入年龄" v-model="newStudent.age">
 </div>
 <div>
 <span>性别:</span>
 <select v-model="newStudent.sex">
  <option value="男">男</option>
  <option value="女">女</option>
 </select>
 </div>
 <div>
 <span>手机:</span>
 <input type="text" placeholder="请输入手机号码" v-model="newStudent.phone">
 </div>
 <button @click="createNewStudent()">创建新用户</button>
</fieldset>
<!--第二部分-->
<table>
 <thead>
 <tr>
 <td>姓名</td>
 <td>性别</td>
 <td>年龄</td>
 <td>手机</td>
 <td>删除</td>
 </tr>
 </thead>
 <tbody>
 <tr v-for="(p, index) in persons">
 <td>{{p.name}}</td>
 <td>{{p.sex}}</td>
 <td>{{p.age}}</td>
 <td>{{p.phone}}</td>
 <td>
  <button @click="deleteStudentMsg(index)">删除</button>
 </td>
 </tr>
 </tbody>
</table>
</div>
</template>

<script>
export default {
 name: "todolist2",
 data(){
  return{
  persons: [
   {name: '张三', age: 20, sex: '男', phone: '18932323232'},
   {name: '李四', age: 30, sex: '男', phone: '18921212122'},
   {name: '王五', age: 20, sex: '男', phone: '18932223232'},
   {name: '赵六', age: 25, sex: '女', phone: '18932322232'},
  ],
  newStudent: {name: '', age: 0, sex: '男', phone: ''}
  }
 },
 methods: {
 // 创建一条新纪录
 createNewStudent(){
  // 姓名不能为空
  if(this.newStudent.name === ''){
  alert('姓名不能为空');
  return;
  }

// 年龄不能小于0
  if(this.newStudent.age <= 0){
  alert('请输入正确的年龄');
  return;
  }

// 手机号码
  if(this.newStudent.phone === ''){
  alert('手机号码不正确');
  return;
  }

// 往数组中添加一条新纪录
  this.persons.unshift(this.newStudent);
  // 清空数据
  this.newStudent = {name: '', age: 0, sex: '男', phone: ''}
 },

// 删除一条学生纪录
 deleteStudentMsg(index){
  this.persons.splice(index,1);
 }
 },
}
</script>

<style scoped>
#app{
margin: 50px auto;
width: 600px;
}

fieldset{
border: 1px solid orangered;
margin-bottom: 20px;
}

fieldset input{
width: 200px;
height: 30px;
margin: 10px 0;
}

table{
width: 600px;
border: 2px solid orangered;
text-align: center;
}

thead{
background-color: orangered;
}
</style>

来源:https://blog.csdn.net/weixin_38404899/article/details/87934786

标签:vue,表单录入
0
投稿

猜你喜欢

  • Pytorch转tflite方式

    2023-08-10 03:47:29
  • ASP访问带多个参数的存储过程

    2008-10-14 16:45:00
  • IE6终极备忘单——策略

    2010-01-13 13:05:00
  • Vue2.0在IE11版本浏览器中的兼容性问题

    2024-04-29 13:08:55
  • python网络爬虫学习笔记(1)

    2023-12-24 04:08:16
  • Python Tkinter简单布局实例教程

    2021-03-08 13:41:23
  • Mysql 默认字符集设置方法(免安装版)

    2024-01-24 10:43:31
  • python基于Opencv实现人脸口罩检测

    2021-11-18 02:45:16
  • Python+OCR实现文档解析的示例代码

    2023-11-22 02:34:19
  • 解决Jupyter无法导入已安装的 module问题

    2022-05-13 07:14:18
  • 在Linux下安装Oracle

    2010-07-30 12:46:00
  • JavaScript正则表达式的贪婪匹配和非贪婪匹配

    2024-04-30 09:53:01
  • python学习之面向对象【入门初级篇】

    2023-11-19 19:59:44
  • Jquery 改变radio/checkbox选中状态,获取选中的值(示例代码)

    2024-04-22 22:33:33
  • 解析PyCharm集成GitLab代码仓的问题

    2022-06-08 07:36:15
  • MySQL中SQL的单字节注入与宽字节注入

    2009-03-25 14:49:00
  • Python中标准库OS的常用方法总结大全

    2021-04-29 06:43:41
  • Python logging日志库空间不足问题解决

    2021-08-24 15:59:17
  • Python实现 PS 图像调整中的亮度调整

    2021-04-02 20:08:54
  • 解决使用OpenCV中的imread()内存报错问题

    2022-06-06 03:14:21
  • asp之家 网络编程 m.aspxhome.com