支付宝小程序表单组件 表单·Form

2020-09-19 11:15 更新

表单。用于将组件内的用户输入的 textareaswitchinputcheckboxsliderradiopicker等组件提交。

当点击 form 表单中 form-type 为 submit 的 button 组件时,会将表单组件值进行提交,需要在表单组件中加上 name 来作为 key。

说明:

  • 预览效果建议以真机为准。
  • 目前还不支持form表单渲染。
  • formId 需要真机调试才会有返回值。

扫码体验

示例代码

  1. <!-- API-DEMO page/component/form/form.axml -->
  2. <view class="page">
  3. <view class="page-description">表单</view>
  4. <form onSubmit="onSubmit" onReset="onReset">
  5. <view class="page-section">
  6. <view class="page-section-title">Slider</view>
  7. <view class="page-section-demo">
  8. <slider value="80" name="slider" show-value />
  9. </view>
  10. </view>
  11. <view class="page-section">
  12. <view class="form-row">
  13. <view class="form-row-label">Switch</view>
  14. <view class="form-row-content" style="text-align: right">
  15. <switch name="switch" />
  16. </view>
  17. </view>
  18. <view class="form-line" />
  19. <view class="form-row">
  20. <view class="form-row-label">Input</view>
  21. <view class="form-row-content">
  22. <input name="input" class="input" placeholder="input something"/>
  23. </view>
  24. </view>
  25. </view>
  26. <view class="page-section">
  27. <view class="page-section-title">Radio</view>
  28. <view class="page-section-demo">
  29. <radio-group name="radio-group">
  30. <label><radio value="radio1" />radio1</label>
  31. <label><radio value="radio2" />radio2</label>
  32. </radio-group>
  33. </view>
  34. </view>
  35. <view class="page-section">
  36. <view class="page-section-title">Checkbox</view>
  37. <view class="page-section-demo">
  38. <checkbox-group name="checkbox">
  39. <label><checkbox value="checkbox1" />checkbox1</label>
  40. <label><checkbox value="checkbox2" />checkbox2</label>
  41. </checkbox-group>
  42. </view>
  43. <view class="page-section-btns">
  44. <view><button type="ghost" size="mini" formType="reset">Reset</button></view>
  45. <view><button type="primary" size="mini" data-id="121" formType="submit">Submit</button></view>
  46. </view>
  47. </view>
  48. </form>
  49. </view>
  50. // API-DEMO page/component/form/form.js
  51. Page({
  52. data: {},
  53. onSubmit(e) {
  54. my.alert({
  55. content: `数据:${JSON.stringify(e.detail.value)}`,
  56. });
  57. },
  58. onReset() {
  59. },
  60. });
  61. /* API-DEMO page/component/form/form.acss */
  62. button + button {
  63. margin-top: 32rpx;
  64. }

属性

属性 类型 默认值 描述 最低版本
report-submit boolean - onSubmit 回调是否返回 formId,用于发送 模板消息,使用前可使用 canIUse ('form.report-submit')判断是否支持。 1.3.0
onSubmit EventHandle - 携带 form 中的数据触发 submit 事件,event.detail = {value : {'slider': '80'}, buttonTarget: {'dataset': 'buttonDataset'} } (可以在 submit 按钮上添加自定义参数)。 buttonTarget 1.7.0.开始支持
onReset EventHandle - 表单重置时会触发 reset 事件。 -

常见问题

formId 返回值是否可以自定义?

formId 返回值不支持自定义,设置完成对应属性 report-submit 后支付宝返回。

支付宝小程序消息推送获取的 formId 有效期是多久?用一次会失效一次吗?

formId 有效期是7天,可在 7 天内向用户推送消息。一个 formId 可发送三次。

小程序接入,付款后调用消息下发返回 formId 为何显示不合法?

商户的模板是表单类型,表单类的模板消息只允许使用表单组件生成的 formId 发送。

小程序 form 表单静默触发吗?

不支持,需要用户点击触发。

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号