天天看點

微信小程式擷取form表單資料

初心-楊瑞超個人部落格誠邀您加入qq群(IT-程式猿-技術交流群):757345416

form概述

表單,将元件内的使用者輸入的 送出。

當點選 表單中 form-type 為 submit 的 元件時,會将表單元件中的 value 值進行送出,需要在表單元件中加上 name 來作為 key。

直接貼代碼了:

<form bindsubmit="formSubmit" bindreset="formReset">
  <view class="section section_gap">
    <view class="section__title">switch</view> <switch name="switch" />
  </view>
  <view class="section section_gap">
    <view class="section__title">slider</view>
    <slider name="slider" show-value></slider>
  </view>

  <view class="section">
    <view class="section__title">input</view>
    <input name="input" placeholder="please input here" />
  </view>
  <view class="section section_gap">
    <view class="section__title">radio</view>
    <radio-group name="radio-group">
      <label><radio value="radio1" />radio1</label>
      <label><radio value="radio2" />radio2</label>
    </radio-group>
  </view>
  <view class="section section_gap">
    <view class="section__title">checkbox</view>
    <checkbox-group name="checkbox">
      <label><checkbox value="checkbox1" />checkbox1</label>
      <label><checkbox value="checkbox2" />checkbox2</label>
    </checkbox-group>
  </view>
  <view class="btn-area">
    <button form-type="submit">Submit</button>
    <button form-type="reset">Reset</button>
  </view>
</form>
           
Page({
  formSubmit(e) {
    console.log('form發生了submit事件,攜帶資料為:', e.detail.value)
  },
  formReset() {
    console.log('form發生了reset事件')
  }
})