小程序模板网

微信小程序问题汇总及详解《六》获取form表单初始值提交数据 ...

2018-04-17 活寃业

分享者:JoyJin,来自原文地址

 

				
  1. <form bindsubmit="formSubmit">
  2. <view class="txt">
  3. <view class="ima"></view>
  4. <view class="txt2">姓名</view>
  5. <input placeholder="请输入姓名" maxlength="10" class="txt3" value="{{mem_name}}" bindchange="name" name="name2"/>
  6. </view>
  7. <view class="txt">
  8. <view class="ima"></view>
  9. <view class="txt2">身份证号</view>
  10. <input type="idcard" placeholder="请输入身份证号码" class="txt3" value="{{mem_IDnum}}" bindchange="ID_num" maxlength="18" name="ID_num2"/>
  11. </view>
  12. <view style="clear:both"></view>
  13. <button class="btn" form-type="submit" hover-class="none">完成</button>
  14. </form>
 

				
  1. page({
  2. name: function (e) { //获取input输入的值
  3. var that = this;
  4. that.setData({
  5. name: e.detail.value
  6. })
  7. },
  8. ID_num: function (e) { //获取input输入的值
  9. var that = this;
  10. that.setData({
  11. ID_num: e.detail.value
  12. })
  13. var id_num = that.data.ID_num
  14. if (!(id_num.length === 15 || id_num.length === 18)) {
  15. wx.showToast({
  16. title: '请输入15或18位数身份证号码',
  17. image: '../Image/error.png',
  18. duration: 2000
  19. })
  20. }
  21. },
  22.  
  23. formSubmit: function (e) {
  24. var that = this;
  25. var tokend = wx.getStorageSync('tokend')
  26. var name2 = e.detail.value.name2; //获取input初始值
  27. var ID_num2 = e.detail.value.ID_num2; //获取input初始值
  28. var name = that.data.name ? that.data.name : name2 //三元运算,如果用户没修改信息,直接提交原来的信息,如果用户修改了信息,就将修改了的信息和未修改过的信息一起提交
  29. var ID_num = that.data.ID_num ? that.data.ID_num : ID_num2
  30. wx.request({
  31. method: 'POST',
  32. url: 'https://....?token=' + tokend, //接口地址
  33. data: {
  34. 'name': name,
  35. 'ID_num': ID_num
  36. },
  37. header: { 'content-type': 'application/json' },
  38. success: function (res) {
  39. wx.showToast({
  40. title: '资料修改成功',
  41. image: '../Image/suess.png',
  42. duration: 2000
  43. })
  44. setTimeout(function () {
  45. wx.switchTab({
  46. url: '../index/index',
  47. })
  48. }, 2000)
  49.  
  50. },
  51. fail: function (res) {
  52. console.log('cuowu' + ':' + res)
  53. }
  54. })
  55. },
  56. })


易优小程序(企业版)+灵活api+前后代码开源 码云仓库:starfork
本文地址:https://www.eyoucms.com/wxmini/doc/course/23518.html 复制链接 如需定制请联系易优客服咨询:800182392 点击咨询
QQ在线咨询