微信小程序的form表单提交

获取input有两种方法:

第一:bindsubmit方法

注意:

1.使用form里面定义bindsubmit事件

2.bindsubmit事件需要配合button里面定义的formType="submit" 操作

3.设置input的name值来获取对应的数据

html 复制代码
<form bindsubmit="formSubmit">
	<input type="text" name="name" class="content" placeholder="请输入司机姓名" />
	<input type="text" name="plateNo" class="content" placeholder="请输入车牌号" />
	<button class="wehx-foot-btn" hover-class="wehx-button_hover" formType="submit">确定</button>
</form>

通过e.detail.value获取数据, 其中包含input的value值

javascript 复制代码
formSubmit: function (e) {
    console.log(e.detail.value)
    let name= data.detail.value.name
    let plateNo= data.detail.value.plateNo;
}

第二种:bindinput方法

注意:

1.在input框内使用bindinput属性的方式定义事件名称

2.事件是光标移动发生数据改变,数据自动获取

html 复制代码
<input type="text" bindinput='getInputName'  name="name" class="content" placeholder="请输入司机姓名" />

通过e.detail获取数据, 其中包含input的value值、光标的位置cursor

javascript 复制代码
getInputName:function(e){
	console.log(e.detail)
	// 获取到input的值
	let name = e.detail.value;
	// 获取到光标的位置
	let local = e.detail.cursor;
}

参考:微信小程序之 获取input框输入值

form表单提交

html 复制代码
<form bindsubmit="formSubmit" bindreset="formReset">
  <view class="section section_gap">
    <view class="section__title">是否公开信息</view>
    <switch name="isPub" />
  </view>
 
  <view class="section">
    <view class="section__title">手机号</view>
    <input name="phone" placeholder="手机号" />
  </view>
  <view class="section">
    <view class="section__title">密码</view>
    <input name="pwd" placeholder="密码" password/>
  </view>
  <view class="section section_gap">
    <view class="section__title">性别</view>
    <radio-group name="sex">
      <label>
        <radio value="男" checked/>男</label>
      <label>
        <radio value="女" />女</label>
    </radio-group>
  </view>
  <view class="btn-area">
    <button formType="submit">提交</button>
    <button formType="reset">重置</button>
  </view>
</form>
<view wx:if="{{isSubmit}}">
  {{warn ? warn : "是否公开信息:"+isPub+",手机号:"+phone+",密码:"+pwd+",性别:"+sex}}
</view>
css 复制代码
.section{
  display: flex;
  flex-direction: row;
  margin: 20rpx;
}
.section view{
  margin-right: 20rpx;
}
.btn-area{
  margin: 20rpx;
}
button{
  margin: 10rpx 0;
}
javascript 复制代码
let app = getApp();
Page({
  data: {
    isSubmit: false,
    warn: "",
    phone: "",
    pwd: "",
    isPub: false,
    sex: "男"
  },
  formSubmit: function (e) {
    console.log('form发生了submit事件,携带数据为:', e.detail.value);
    let { phone, pwd, isPub, sex } = e.detail.value;
    if (!phone || !pwd) {
      this.setData({
        warn: "手机号或密码为空!",
        isSubmit: true
      })
      return;
    }
    this.setData({
      warn: "",
      isSubmit: true,
      phone,
      pwd,
      isPub,
      sex
    })
  },
  formReset: function () {
    console.log('form发生了reset事件')
  }
})

参考:微信小程序-form表单提交

注意几个点:

实现过程分为以下几步

1.给 form 表单设置 bindsubmit 属性。

2.给所有 input / check等等项 设置 name 属性 (否则无法获取值)

3.给按钮设置 form-type="submit",与第一步 form 设置的 bindsubmit 属性值 绑定

4.编写按钮触发的函数 (第一步与第三步共同绑定的)

相关推荐
HERR_QQ14 小时前
【unify】unify的微信小程序开发学习 (to be continued)
学习·微信小程序·小程序
racerun1 天前
小程序导航设置更多内容的实现方法
小程序
说私域1 天前
基于开源AI智能名片链动2+1模式S2B2C商城小程序的超级文化符号构建路径研究
人工智能·小程序·开源
mg6681 天前
微信小程序入门实例_____快速搭建一个快递查询小程序
微信小程序·小程序
程序员柳1 天前
基于微信小程序的校园二手交易平台、微信小程序校园二手商城源代码+数据库+使用说明,layui+微信小程序+Spring Boot
数据库·微信小程序·layui
Jyywww1211 天前
微信小程序学习笔记
笔记·学习·微信小程序
The_era_achievs_hero1 天前
微信小程序41~50
微信小程序·小程序
走,带你去玩1 天前
uniapp 微信小程序水印
微信小程序·小程序·uni-app
是一碗螺丝粉1 天前
🔥 微信H5视频自动播放终极秘籍:WeixinJSBridge竟是官方“通行证”?
微信小程序