微信小程序提交form表单内容

矫情吗;* 2022-10-10 04:44 250阅读 0赞

wxml

  1. <form catchsubmit="confirmPublish">
  2. <view class="cu-form-group margin-top">
  3. <view class="title d-flex">
  4. 姓名
  5. </view>
  6. <view class="w-100 h-100 borderBottom flex-1 d-flex jc-start ai-center" style="height:100rpx;">
  7. <input maxlength="15" placeholder="请输入姓名" value="{
  8. {name}}" data-name="name" bindblur="setInput"></input>
  9. </view>
  10. </view>
  11. <view class="cu-form-group margin-top">
  12. <view class="title d-flex">性别
  13. </view>
  14. <picker class="w-100 h-100 borderBottom flex-1 d-flex jc-start ai-center" style="height:100rpx;font-size: 30rpx;color: #555;" bindchange="bindPickerChange" value="{
  15. {index}}" range="{
  16. {array}}">
  17. <view class="picker">
  18. {
  19. {array[sex]}}
  20. </view>
  21. </picker>
  22. </view>
  23. <view class="cu-form-group margin-top">
  24. <view class="title d-flex">
  25. 手机号
  26. </view>
  27. <view class="w-100 h-100 borderBottom flex-1 d-flex jc-start ai-center" style="height:100rpx;">
  28. <input maxlength="15" placeholder="请输入姓名" value="{
  29. {phone}}" data-name="phone" bindblur="setInput"></input>
  30. </view>
  31. </view>
  32. <view class="cu-form-group margin-top">
  33. <view class="title d-flex">
  34. 证件号
  35. </view>
  36. <view class="w-100 h-100 borderBottom flex-1 d-flex jc-start ai-center" style="height:100rpx;">
  37. <input maxlength="15" placeholder="请输入姓名" value="{
  38. {idNumber}}" data-name="idNumber" bindblur="setInput"></input>
  39. </view>
  40. </view>
  41. <view class="cu-form-group margin-top">
  42. <view class="title d-flex">日期选择器</view>
  43. <view class="picker" class="w-100 h-100 borderBottom flex-1 d-flex jc-start ai-center" style="height:100rpx;font-size: 30rpx;color: #555;" bind:tap="openPicker">
  44. {
  45. {currentChoose}}
  46. </view>
  47. </view>
  48. <van-action-sheet show="{
  49. { show }}" bind:close="onClose" bind:getuserinfo="onGetUserInfo">
  50. <van-datetime-picker type="datetime" value="{
  51. { currentDate }}" min-date="{
  52. { minDate }}" max-date="{
  53. { maxDate }}" bind:cancel="onCancel" bind:confirm="onConfirm" bind:change="onChange" />
  54. </van-action-sheet>
  55. <view class="cu-form-group margin-top">
  56. <view class="title d-flex">来访事由
  57. </view>
  58. <picker class="w-100 h-100 borderBottom flex-1 d-flex jc-start ai-center" style="height:100rpx;font-size: 30rpx;color: #555;" bindchange="bindPickerChangeCause" value="{
  59. {index1}}" range="{
  60. {visitCausearray}}">
  61. <view class="picker">
  62. {
  63. {visitCausearray[visitCause]}}
  64. </view>
  65. </picker>
  66. </view>
  67. <view class="cu-form-group margin-top">
  68. <view class="title d-flex">
  69. 来访公司
  70. </view>
  71. <view class="w-100 h-100 borderBottom flex-1 d-flex jc-start ai-center" style="height:100rpx;">
  72. <input maxlength="15" placeholder="请输入姓名" value="{
  73. {visitCompany}}" data-name="visitCompany" bindblur="setInput"></input>
  74. </view>
  75. </view>
  76. <view class="cu-form-group margin-top">
  77. <view class="title d-flex">
  78. 被访姓名
  79. </view>
  80. <view class="w-100 h-100 borderBottom flex-1 d-flex jc-start ai-center" style="height:100rpx;">
  81. <input maxlength="15" placeholder="请输入姓名" value="{
  82. {interviewee}}" data-name="interviewee" bindblur="setInput"></input>
  83. </view>
  84. </view>
  85. <view class="cu-form-group margin-top">
  86. <view class="title d-flex">被访部门
  87. </view>
  88. <picker class="w-100 h-100 borderBottom flex-1 d-flex jc-start ai-center" style="height:100rpx;font-size: 30rpx;color: #555;" bindchange="bindPickerOrgan" value="{
  89. {index2}}" range="{
  90. {organarray}}">
  91. <view class="picker">
  92. {
  93. {organarray[visitOrganId]}}
  94. </view>
  95. </picker>
  96. </view>
  97. <button class='btn1' bindtap="bindViewTapsuccess" form-type="submit">完成</button>
  98. </form>

js

  1. // pages/addsign/addsign.js
  2. var util = require('../../utils/util.js');
  3. const app = getApp()
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. globalid: [],
  10. array: ['男', '女'],
  11. sex: 0,
  12. visitCausearray: ['面试', '开会', '拜访顾客', '项目实施', '其他'],
  13. visitCause: 0,
  14. organarray: ['生产部', '运营部', '营销部'],
  15. visitOrganId: 0,
  16. date: '2021-06-01',
  17. name: '',
  18. phone: '',
  19. idNumber: '',
  20. subscribeTime: '',
  21. visitCompany: '',
  22. interviewee: '',
  23. visitOrgan: '',
  24. //日期选择器
  25. minHour: 0,
  26. maxHour: 24,
  27. minDate: new Date(1990, 1, 1).getTime(),
  28. maxDate: new Date(2099, 12, 31).getTime(),
  29. currentDate: new Date().getTime(),
  30. show: false,
  31. currentChoose: ''
  32. },
  33. //日期选择器的处理
  34. openPicker() {
  35. this.setData({
  36. show: true
  37. })
  38. },
  39. onConfirm(e) {
  40. this.setData({
  41. show: false,
  42. currentChoose: this.formatDate(new Date(e.detail))
  43. })
  44. },
  45. onClose() {
  46. this.setData({
  47. show: false
  48. })
  49. },
  50. onCancel() {
  51. this.setData({
  52. show: false
  53. })
  54. },
  55. formatDate(date) {
  56. let taskStartTime
  57. if (date.getMonth() < 9) {
  58. taskStartTime = date.getFullYear() + "-0" + (date.getMonth() + 1) + "-"
  59. } else {
  60. taskStartTime = date.getFullYear() + "-" + (date.getMonth() + 1) + "-"
  61. }
  62. if (date.getDate() < 10) {
  63. taskStartTime += "0" + date.getDate()
  64. } else {
  65. taskStartTime += date.getDate()
  66. }
  67. taskStartTime += " " + date.getHours() + ":" + date.getMinutes()
  68. this.setData({
  69. taskStartTime: taskStartTime,
  70. })
  71. return taskStartTime;
  72. },
  73. //男女
  74. bindPickerChange: function(e) {
  75. console.log('picker发送选择改变,携带值为', e.detail.value)
  76. this.setData({
  77. sex: e.detail.value
  78. })
  79. },
  80. //来访事由
  81. bindPickerChangeCause: function(e) {
  82. console.log('picker发送选择改变,携带值为', e.detail.value)
  83. this.setData({
  84. visitCause: e.detail.value
  85. })
  86. },
  87. //部门
  88. bindPickerOrgan: function(e) {
  89. console.log('picker发送选择改变,携带值为', e.detail.value)
  90. this.setData({
  91. visitOrganId: e.detail.value
  92. })
  93. },
  94. /**
  95. * 生命周期函数--监听页面加载
  96. */
  97. onLoad: function(options) {
  98. // 调用函数时,传入new Date()参数,返回值是日期和时间
  99. var time = util.formatTime(new Date());
  100. this.setData({
  101. currentChoose: time
  102. });
  103. },
  104. setInput: function(e) {
  105. const {
  106. name
  107. } = e.target.dataset
  108. this.data[name] = e.detail.value
  109. this.setData(this.data)
  110. },
  111. confirmPublish: function() {
  112. var that = this
  113. const data = {}
  114. data.name = this.data.name
  115. data.sex = this.data.sex
  116. data.phone = this.data.phone
  117. data.idNumber = this.data.idNumber
  118. data.subscribeTime = this.data.currentChoose
  119. data.visitCause = this.data.visitCause
  120. data.visitCompany = this.data.visitCompany
  121. data.interviewee = this.data.interviewee
  122. data.visitOrgan = this.data.visitOrgan
  123. data.visitOrganId = this.data.visitOrganId
  124. console.log(JSON.stringify(data))
  125. wx.request({
  126. url: 'http://192.xxx.4.1xx:8093/cs-applet/subscribe/addCallerSubscribe',
  127. method: 'POST',
  128. data: data,
  129. header: {
  130. 'content-type': 'application/json',
  131. },
  132. success: function(res) {
  133. },
  134. fail: function(error) {
  135. wx.showToast({
  136. title: error.message || '保存失败'
  137. })
  138. console.log(error)
  139. }
  140. })
  141. },
  142. })

css

  1. /* 确认发布的按钮 */
  2. .btn1 {
  3. width: 90%;
  4. margin-top: 70rpx;
  5. margin-bottom: 70rpx;
  6. background-color: #09bb07;
  7. color: white;
  8. font-size: 33rpx;
  9. border-radius: 13rpx;
  10. display: flex;
  11. flex-direction: row;
  12. align-items: center;
  13. justify-content: center;
  14. }
  15. .d-flex {
  16. display: flex;
  17. }
  18. .cu-form-group .title {
  19. text-align: justify;
  20. padding-right: 30rpx;
  21. font-size: 30rpx;
  22. position: relative;
  23. height: 60rpx;
  24. line-height: 60rpx;
  25. }
  26. .cu-form-group {
  27. background-color: var(--white);
  28. padding: 1rpx 30rpx;
  29. display: flex;
  30. align-items: center;
  31. min-height: 100rpx;
  32. justify-content: space-between;
  33. }
  34. .borderBottom {
  35. border-bottom: 1rpx solid #eee;
  36. }
  37. .ai-center {
  38. align-items: center;
  39. }
  40. .jc-start {
  41. justify-content: flex-start;
  42. }
  43. .flex-1 {
  44. flex: 1;
  45. }
  46. .cu-form-group input {
  47. flex: 1;
  48. font-size: 30rpx;
  49. color: #555;
  50. padding-right: 20rpx;
  51. }

点击完成的时候,将所有填写的值传给后端

e896b9da0d77839c5dde9462ce99874e.png

发表评论

表情:
评论列表 (有 0 条评论,250人围观)

还没有评论,来说两句吧...

相关阅读

    相关 程序验证

    由于微信小程序表单组件没有自带的验证功能,因此要针对每一项进行判断比较繁琐 因此封装了一个表单验证js, 具体验证规则根据自己所用到的进行添加,具体案例代码片段点击链接[微信小