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

uniapp如何添加多个表单数组?

目录

一、实现思路

二、实现步骤

        ①view部分展示

        ②JavaScript 内容

        ③css中样式展示

        

三、效果展示

四、小结 + 注意事项

        总结模板:


一、实现思路

        1.在 data 中定义一个数组,用于存储表单项的数据

        2.在模板中使用 v-for 指令渲染表单项

        3.在 methods 中定义添加和删除表单项的方法

        每点击一次 “添加表单项” 按钮,就会新增一个表单项

        并且你可以通过输入框的 v-model 来动态修改表单项的值。点击对应的 “删除” 按钮可以移除对应的表单项。

        

二、实现步骤

        ①view部分展示

<view style="background-color: #e5e5e5;padding: 32rpx;"><view class="publish-top-two" style="margin: 32rpx; "><template v-for="(item,index) in exigencyList"><view class="publish-top" style="position: relative; padding: 32rpx;" :key="index"><view class="upload-title">紧急联系人{{ item.num }}</view><view class="person-item"><view class="flex"><view class="asterisk">*</view><view class="item-left">姓名</view></view><u-input v-model="item.name" border="none" placeholder="请输入姓名"placeholderStyle="color: #999"></u-input></view><view class="person-item"><view class="flex"><view class="asterisk">*</view><view class="item-left">手机号</view></view><u-input v-model="item.telephone" border="none" placeholder="请输入手机号"placeholderStyle="color: #999"></u-input></view><view class="person-item"><view class="flex"><view class="asterisk">*</view><view class="item-left">关系</view></view><u-input v-model="item.relation" border="none" placeholder="请输入关系"placeholderStyle="color: #999"></u-input></view><view style="position: absolute;top: -5px;left: -5px;width: 40rpx;height: 40rpx;"v-if="index != 0" @click.stop="reduceGoods(index)"><!-- 								<u-icon name="minus-circle" color="Error" size="22"></u-icon> --><image style="width: 100%;height: 100%;"src="../../pagesLeave/static/information/reduce.png" mode="aspectFill"></image></view></view></template></view><view class="addexigencybth" @click="getadd">添加联系人</view></view>

        ②JavaScript 内容

<script>export default {data() {return {exigencyList: [{num: '1',name: '',telephone: '',relation: '',},{num: '2',name: '',telephone: '',relation: '',}],}},methods: {// 添加紧急联系人getadd() {this.exigencyList.push({num: '1',name: '',telephone: '',relation: '',})//新增默认加 0.1// this.form.salary += 0.1},//减少紧急联系人reduceGoods(index) {this.exigencyList.splice(index, 1)// 需要去除减少物品的价格let count = 0for (let i = 0; i < this.tabList; i++) {count += this.tabList[i].fetchMoney}// this.form.salary = count},}}
</script>

        ③css中样式展示

<style lang="scss" scoped>.publish-top {margin: 12px 16px 0px 16px;background-color: #fff;border-radius: 16rpx;.person-item {display: flex;align-items: center;padding: 32rpx;border-bottom: 1px solid #E6E6E8;}//紧急联系人.publish-top-two {position: absolute;left: auto;top: 104px;.publish-top {position: relative;background-color: #fff;border-radius: 16rpx;.reduce-btn {position: absolute;top: 0px;left: 0px;width: 40rpx;height: 40rpx;image {width: 100%;height: 100%;}}}}}.asterisk {color: rgba(255, 128, 128, 1);margin-right: 10rpx;margin-top: 12rpx;}.item-left {color: #1A1A1A;font-family: MiSans-Medium, MiSans;font-weight: 500;font-size: 32rpx;margin-right: 32rpx;}.item-right {text-align: right;font-size: 32rpx;color: #333}.addexigencybth {height: 88rpx;margin: 12px 16px 0px 16px;background-color: #fff;border-radius: 16rpx;display: flex;align-items: center;justify-content: center;color: #1A1A1A;font-size: 32rpx;font-weight: 500;}
</style>

        

三、效果展示

        点击添加按钮就会添加一个输入框

        点击减少,就会减少输入框

       

        

四、小结 + 注意事项

        总结模板:

 在模板中使用 v-for 指令渲染表单项 <div>
    <div v-for="(item, index) in formItems" :key="index">
      <input type="text" v-model="item.value">
      <button @click="removeFormItem(index)">删除</button>
    </div>
    <button @click="addFormItem">添加表单项</button>
  </div>

  在 data 中定义一个数组,用于存储表单项的数据
data() {
  return {
    formItems: []
  }
}

在 methods 中定义添加和删除表单项的方法

methods: {
  addFormItem() {
    this.formItems.push({ value: '' });
  },
  removeFormItem(index) {
    this.formItems.splice(index, 1);
  }
}

        这样就完成了在uni-app中添加多个表单数组的功能。每次点击“新增”按钮时,会自动添加一个新的表单项;而点击已存在的表单项右侧的“删除”按钮时,则会从列表中移除该表单项。

        表单中至少保留一条表单项,必须要有添加按钮,和移除按钮

相关文章:

  • iPhone 14支持NFC吗?如果支持,那么怎么启用
  • 华为radius认证
  • Qt 信号与槽
  • 时序数据库 Tdengine 执行命令能够查看执行的sql语句
  • Matlab plot绘图的 title 语法
  • 前端怎么监听手机键盘是否弹起
  • QGIS编译(跨平台编译)之二十七:giflib编译(Windows、Linux、MacOS环境下编译)
  • 高考复习技巧考研资料、美赛论文及代码,数据收集网站(初高中招生考试全科试卷等)
  • 视觉检测系统:工厂生产零部件的智能检测
  • protobuf-go pragma.go 文件介绍
  • 如何用Docker+jenkins 运行 python 自动化?
  • excel中去掉单元格中两个数字之间的空格
  • 《HelloGitHub》第 94 期
  • C语言王道第八周一题
  • Android 8.1 相关修改
  • [笔记] php常见简单功能及函数
  • 【每日笔记】【Go学习笔记】2019-01-10 codis proxy处理流程
  • 【跃迁之路】【463天】刻意练习系列222(2018.05.14)
  • 2019.2.20 c++ 知识梳理
  • CentOS 7 修改主机名
  • CentOS7简单部署NFS
  • Dubbo 整合 Pinpoint 做分布式服务请求跟踪
  • Electron入门介绍
  • ERLANG 网工修炼笔记 ---- UDP
  • JS数组方法汇总
  • storm drpc实例
  • windows下mongoDB的环境配置
  • 道格拉斯-普克 抽稀算法 附javascript实现
  • 电商搜索引擎的架构设计和性能优化
  • 高性能JavaScript阅读简记(三)
  • 个人博客开发系列:评论功能之GitHub账号OAuth授权
  • 使用Gradle第一次构建Java程序
  • 树莓派 - 使用须知
  • 数据科学 第 3 章 11 字符串处理
  • 延迟脚本的方式
  • 最简单的无缝轮播
  • python最赚钱的4个方向,你最心动的是哪个?
  • # 达梦数据库知识点
  • ###51单片机学习(2)-----如何通过C语言运用延时函数设计LED流水灯
  • #NOIP 2014# day.2 T2 寻找道路
  • #QT(一种朴素的计算器实现方法)
  • #基础#使用Jupyter进行Notebook的转换 .ipynb文件导出为.md文件
  • #图像处理
  • #中国IT界的第一本漂流日记 传递IT正能量# 【分享得“IT漂友”勋章】
  • (1)STL算法之遍历容器
  • (4)事件处理——(7)简单事件(Simple events)
  • (libusb) usb口自动刷新
  • (Redis使用系列) SpirngBoot中关于Redis的值的各种方式的存储与取出 三
  • (附源码)ssm码农论坛 毕业设计 231126
  • (七)Knockout 创建自定义绑定
  • (三)Honghu Cloud云架构一定时调度平台
  • (十七)Flask之大型项目目录结构示例【二扣蓝图】
  • .bat批处理(七):PC端从手机内复制文件到本地
  • .NET Core MongoDB数据仓储和工作单元模式封装
  • .NET Core 版本不支持的问题