小程序表单提交功能实现

作为一个小企业开发人员,经常被领导要求开发这个开发那个,我掰着手指头数了数,开发生涯大致是这样的:
C++》php》前端》C#》golang》小程序
真是命运坎坷啊!
这不又来指挥我向小程序进军了,咱也不甘示弱,只要money到位了,下海都行、写汇编也可以试试!

说实话以前一直写jquery,最近才搞vue,对于vue和小程序这类的数据绑定有点不适应,可能以后会慢慢喜欢上吧。

一、实现效果图

请添加图片描述

二、代码

  • wxml文件
<view class="container">
  <form bindsubmit="formSubmit">
    <input hidden="true" type="hidden"  name="openid" value="{{openid}}"></input>
    <input hidden="true" type="hidden"  name="id" value="{{id}}"></input>
    <view class="bg-fff p-lr30 border-t">
      <view class="ipt-wrap border-b flex ai-center">
        <label for="" class="font14">主题</label>
        <input type="text" class="ml40 flex1" placeholder="请输入日程主题" name="title" value="{{title}}"></input>
      </view>
      <view class="ipt-wrap border-b">
        <textarea id="" class="textarea _w100 p-tb30 lh10 content_text" maxlength="-1" placeholder="备注" name="remark" value="{{remark}}"></textarea>
      </view>
    </view>

    <!-- 起始时间 -->
    <view class="section  bg-fff p30 flex ai-center jc-sb ">
      <view class="section__title">开始时间</view>
      <picker mode="date"  name="start_date" value="{{start_date}}"  bindchange="bindStartDateChange">
        <view class="picker">
          {{start_date}}
        </view>
      </picker>|
      <picker 
      mode="time" 
      name="start_time" 
      value="{{start_time}}"  
      bindchange="bindStartTimeChange"
      >
        <view class="picker">
          {{start_time}}
        </view>
      </picker>
    </view>


    <!-- 结束时间 -->
    <view class="section  bg-fff p30 flex ai-center jc-sb ">
      <view class="section__title">结束时间</view>
      <picker mode="date" value="{{end_date}}" name="end_date" bindchange="bindEndDateChange">
        <view class="picker">
          {{end_date}}
        </view>
      </picker>|
      <picker 
      mode="time" 
      name="end_time" 
      value="{{end_time}}" 
      bindchange="bindEndTimeChange" >
        <view class="picker">
          {{end_time}}
        </view>
      </picker>
    </view>

    <view class="section bg-fff p30 flex ai-center jc-sb ">
      <view class="section__title">分类</view>
      <picker mode="selector" bindchange="bindPickerChange" name="note_type" value="{{index}}" range="{{select}}">
        <view class="picker">
          {{select[select_index]}}
        </view>
      </picker>
    </view>

    <view class=" bg-fff p30 flex ai-center jc-sb ">
      <view class="flex ai-center">
        <label for="" class="font14">是否已完成</label>
      </view>
      <view class="flex">
        <switch class="switch" name="is_done" color="#279efd"></switch>
      </view>
    </view>

    <view class="combtn font16 color-fff _w100 bg-btn">
      <button  class="mini-btn transbg font16  _w100 " form-type='submit'>保存</button>
    </view>
  </form>
</view>
  • js文件
//const moment = require("../../utils/moment.min");
const moment = require("../../utils/moment.min");
moment.locale('en', {
  // longDateFormat: {
  //   l: "YYYY-MM-DD",
  //   L: "YYYY-MM-DD HH:mm"
  // },
  week: {
    dow: 1, //星期的第一天是星期一
    doy: 7 //年份的第一周必须包含1月1日(7+1-1)
  } //定义一周第一天为周一,包含1月1日的周计作该年的第一周
});
// pages/note/add.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    start_date: moment().format("YYYY-MM-DD"),
    end_date: moment().format("YYYY-MM-DD"),
    start_time: moment().format("HH:mm"),
    end_time: moment().add(1, "h").format("HH:mm"),
    list: [],
    select: ['工作日记', '备忘录', '工作计划'],
    select_index: 0,
    remark: "",
    title: "",
    openid: "",
    id: ""
  },
  formSubmit: function (e) {
    //console.log(e.detail)
    var data = e.detail.value
    if (data.title == "") {
      wx.showToast({
        title: '主题不能为空!',
        image: '../../images/icon/warin.png'
      })
      return false;
    }
    wx.request({
      url: 'https://sxxxxxx.com/api/note/add',
      data: data,
      method: 'post',
      dataType: 'json',
      success: function (e) {
        wx.showToast({
          title: '添加成功',
          icon: 'success',
          image: '../../images/icon/success.png',
          duration: 10000,
          complete: res => {
            setTimeout(function () {
              // wx.redirectTo({
              // url: './index'
              // })
              //navigateTo
              wx.reLaunch({
                url: './index'
              })
            }, 2000)
          }
        })

      },
      complete: function () {
        // setTimeout(function () {
        //   wx.hideToast()
        // }, 800)

      }

    })

  },
  bindPickerChange(e) {
    //console.log('分类发送选择改变,携带值为', e.detail)
    this.setData({
      select_index: e.detail.value
    })

  },

  bindStartTimeChange: function (e) {
    this.setData({
      start_time: e.detail.value
    })
  },
  bindEndTimeChange: function (e) {
    this.setData({
      end_time: e.detail.value
    })
  },
  bindStartDateChange: function (e) {
    this.setData({
      start_date: e.detail.value
    })
  },
  bindEndDateChange: function (e) {
    this.setData({
      end_date: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("请求参数为:", options)
    var id = options.id || "";
    if (id != "") {
      //根据id去获取数据库中的记录
      wx.request({
        url: 'https://sxxxxxx.com/api/note/getNoteInfo',
        method: 'post',
        data: {
          'id': id
        },
        success: res => {
          console.log("获取用户数据成功了!", res)
          let tmpData = res.data;
          if (tmpData.code == '1') {
            console.log("获取用户数据成功了!", tmpData)
            this.setData({
              start_date: tmpData.data.start_date,
              end_date: tmpData.data.end_date,
              start_time: tmpData.data.start_time,
              end_time: tmpData.data.end_time,
              title: tmpData.data.title,
              remark: tmpData.data.remark,
              openid: tmpData.data.openid,
              id: tmpData.data.id,
            })
          } else {
            console.log("获取用户数据失败!")
          }
        }
      })
    } else {
      this.setData({
        openid: wx.getStorageSync('openid')
      })
    }
    wx.setNavigationBarTitle({
      title: '日报添加',
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
  • wxss文件
/*  */
.ipt-wrap{
  min-height: 100rpx;
  line-height: 100rpx;
}
.ipt-wrap label{
  min-width: 120rpx;
}
.icon-youjiantou{
  position: relative;
  top: 1rpx;
}
.textarea{
  height: 100rpx;
}
/*swtich样式-start*/
/*swtich整体大小*/
.wx-switch-input{
  width:82rpx !important;
  height:40rpx !important;
}
/*白色样式(false的样式)*/
.wx-switch-input::before{
  width:80rpx !important;
  height: 36rpx !important;
}
/*绿色样式(true的样式)*/
.wx-switch-input::after{
  width: 40rpx !important;
  height: 36rpx !important;
}
.ipt-wrap:last-child{
  border-bottom:none;
}
/*swtich样式end*/
.region{
  width: 500rpx;
  position: absolute;
  left: 150rpx;
}

.transbg{
  background: rgba(0,0,0,0) !important;
  font-size: 45rpx;
  font-family: 'Courier New', Courier, monospace;
  color: aliceblue;
}
.content_text{
  min-height: 200rpx;
  border: 2rpx dashed blanchedalmond;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个使用uniapp实现表单提交的例子。 1. 在`pages`文件夹下创建一个名为`form`的页面。 2. 在`form`页面的`template`标签中,编写表单的HTML代码。例如: ```html <template> <view class="container"> <form @submit.prevent="submitForm"> <view class="form-group"> <label for="name">姓名:</label> <input id="name" type="text" v-model.trim="formData.name" required> </view> <view class="form-group"> <label for="phone">手机号:</label> <input id="phone" type="tel" v-model.trim="formData.phone" required> </view> <view class="form-group"> <label for="email">邮箱:</label> <input id="email" type="email" v-model.trim="formData.email" required> </view> <button type="submit">提交</button> </form> </view> </template> ``` 在上面的代码中,我们使用了`v-model`指令来获取用户输入的数据,并使用`required`属性来设置必填项。 3. 在`form`页面的`script`标签中,定义表单数据的初始值和提交表单的方法。例如: ```javascript <script> export default { data() { return { formData: { name: '', phone: '', email: '' } } }, methods: { submitForm() { // 验证表单数据 if (!this.formData.name) { uni.showToast({ title: '请输入姓名', icon: 'none' }) return } if (!this.formData.phone) { uni.showToast({ title: '请输入手机号', icon: 'none' }) return } if (!this.isValidPhone(this.formData.phone)) { uni.showToast({ title: '手机号格式不正确', icon: 'none' }) return } if (!this.formData.email) { uni.showToast({ title: '请输入邮箱', icon: 'none' }) return } if (!this.isValidEmail(this.formData.email)) { uni.showToast({ title: '邮箱格式不正确', icon: 'none' }) return } // 提交表单数据 uni.showLoading({ title: '提交中...' }) uni.request({ url: 'http://example.com/submit', method: 'POST', data: this.formData, success: (res) => { uni.hideLoading() uni.showToast({ title: '提交成功' }) }, fail: (err) => { uni.hideLoading() uni.showToast({ title: '提交失败', icon: 'none' }) } }) }, isValidPhone(phone) { // 验证手机号格式 // ... return true }, isValidEmail(email) { // 验证邮箱格式 // ... return true } } } </script> ``` 在上面的代码中,我们定义了一个名为`submitForm`的方法来提交表单数据。在该方法中,我们先进行表单数据的验证,如果有任何一项数据验证不通过,则会在页面上弹出错误提示;如果所有数据验证通过,则会通过`uni.request`方法向后端API发送POST请求,提交表单数据。在请求成功或失败后,我们会在页面上弹出相应的提示。 4. 最后,在`form`页面的`style`标签中,添加一些样式,美化表单的显示效果。例如: ```css <style> .container { display: flex; justify-content: center; align-items: center; height: 100vh; } form { display: flex; flex-direction: column; justify-content: center; align-items: center; width: 300rpx; padding: 20rpx; background-color: #fff; border-radius: 10rpx; box-shadow: 0 4rpx 8rpx rgba(0, 0, 0, 0.1); } .form-group { display: flex; width: 100%; margin-bottom: 10rpx; } label { width: 80rpx; font-size: 28rpx; color: #333; text-align: right; margin-right: 20rpx; } input { flex: 1; height: 60rpx; font-size: 28rpx; padding: 10rpx; border: 1rpx solid #ccc; border-radius: 5rpx; outline: none; } button { width: 100%; height: 80rpx; font-size: 32rpx; color: #fff; background-color: #007aff; border: none; border-radius: 5rpx; outline: none; margin-top: 20rpx; } </style> ``` 好了,这样就完成了一个基本的表单提交功能。当用户输入表单数据并点击提交按钮时,我们会先进行数据验证,然后通过API将数据提交到后端服务器。在请求成功或失败后,会在页面上弹出相应的提示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值