189 8069 5689

创新互联百度小程序教程:form表单

  • form 表单
    • 属性说明
      • report-type 有效值
      • report-type 为 subscribe 时,event.detail.formId 说明
      • report-type 为 subscribe 时,status 和 message 具体值
    • 示例
      • 代码示例 1:普通表单
      • 代码示例 2:模板类型表单

    form 表单

    解释:表单,将 form 组件内用户输入 / 选择的提交。当

    表单中 form-type 为 submit 时,点击的
  •  
     
     
    1. Page({
    2. formSubmit(e) {
    3. // 此时 formId 为 'BD0001-formId'(非真实数据)
    4. swan.showModal({
    5. title: 'template-id 为 string',
    6. content: e.detail.formId
    7. });
    8. }
    9. });

    2. 当 template-id 为 Array 时(基础库 3.170.1 开始支持),event.detail 的 formId 为 Object ,其中对象的 key 为 template-id ,value 为其对应的 formId 。举例如下

    • SWAN
    • JS
     
     
     
     
     
     
    1. Page({
    2. data: {
    3. templateId: ['BD0001', 'BD0002']
    4. },
    5. formSubmit(e) {
    6. // 此时 formId 为 {'BD0001': 'BD0001-formId', 'BD0002': 'BD0002-formId'}(非真实数据)
    7. swan.showModal({
    8. title: 'template-id 为 Array',
    9. content: JSON.stringify(e.detail.formId)
    10. });
    11. }
    12. });

    如果 Array 中的 template-id 超过三个,返回的 formId 为空字符串,举例如下:

    • SWAN
    • JS
     
     
     
     
     
     
    1. Page({
    2. data: {
    3. templateId: ['BD0001', 'BD0002', 'BD0003', 'BD0004']
    4. },
    5. formSubmit(e) {
    6. // 此时 formId 为 ''
    7. swan.showModal({
    8. title: 'template-id 超过三个',
    9. content: e.detail.formId
    10. });
    11. }
    12. });

    如果 Array 中有重复的 template-id ,重复的 template-id 对应的 formId 只返回一次,举例如下:

    • SWAN
    • JS
     
     
     
     
     
     
    1. Page({
    2. data: {
    3. templateId: ['BD0001', 'BD0001', 'BD0002']
    4. },
    5. formSubmit(e) {
    6. // 此时 formId 为 {'BD0001': 'BD0001-formId', 'BD0002': 'BD0002-formId'}(非真实数据)
    7. swan.showModal({
    8. title: '有重复的 template-id',
    9. content: JSON.stringify(e.detail.formId)
    10. });
    11. }
    12. });

    注意:在提交 form 表单时,将会弹出模板消息授权弹窗,用户授权后才能在 event.detail 中获取被授权模板消息的 formId 。

    report-type 为 subscribe 时,status 和 message 具体值

    status 为 Number 类型,message 为 String 类型,当用户永久拒绝授权的时候,建议开发者不要再展示订阅消息授权面板入口。

    status message
    500101用户永久拒绝授权
    500102用户单次拒绝授权
    500103用户取消授权
    500104请求模板内容失败
    500105请求 formId 失败

    示例

    跳转编辑工具

    在开发者工具中打开

    在 WEB IDE 中打开

    扫码体验

    代码示例

    请使用百度APP扫码

    代码示例 1:普通表单

    • SWAN
    • JS
     
     
     
    1. bindreset="formReset">
    2. 开关选择器
    3. 开关
    4. 单项选择器
    5. 单选项一
    6. 单选项二
    7. 多项选择器
    8. 多选项一
    9. 多选项二
    10. 多选项三
    11. 滑块选择器
    12. 输入框
    13. 提交表单
     
     
     
    1. Page({
    2. formSubmit(e) {
    3. console.log('form发生了submit事件,携带数据为:', e.detail.value);
    4. swan.showModal({
    5. content: '数据:' + JSON.stringify(e.detail.value),
    6. confirmText: '确定'
    7. });
    8. },
    9. formReset(e) {
    10. console.log('form表单发生了', e.type);
    11. }
    12. });

    代码示例 2:模板类型表单

    在开发者工具中打开

    在开发者工具中打开

    在 WEB IDE 中打开

    • SWAN
    • JS
     
     
     
    1. report-submit
    2. report-type="subscribe"
    3. template-id="BD0003"
    4. subscribe-id="8026"
    5. bindsubmit="reportFormSubmit"
    6. bindreset="reportFormReset">
    7. report-type为subscribe
    8. report-submit
    9. report-type="default"
    10. bindsubmit="reportFormSubmit"
    11. bindreset="reportFormReset">
    12. report-type为default
     
     
     
    1. Page({
    2. onLoad() {
    3. swan.showToast({
    4. title: '此组件需要登录态,请先点击下方的按钮登录',
    5. icon: 'none'
    6. })
    7. },
    8. login(e) {
    9. // 此组件需要在登陆态下使用
    10. console.log('登录信息:', e);
    11. if (e.detail.errCode === '10004') {
    12. swan.showToast({
    13. title: '用户未登录',
    14. icon: 'none'
    15. });
    16. return;
    17. }
    18. swan.showToast({
    19. title: '用户登录成功',
    20. icon: 'none'
    21. });
    22. },
    23. formSubmit(e) {
    24. swan.showModal({
    25. title: '表单数据',
    26. content: JSON.stringify(e.detail.message) + '/' +JSON.stringify(e.detail.status),
    27. confirmText: '确定',
    28. showCancel: false
    29. });
    30. }
    31. });

    文章题目:创新互联百度小程序教程:form表单
    浏览路径:http://www.cdxtjz.cn/article/dhgspjo.html

    联系我们

    您好HELLO!
    感谢您来到成都网站建设公司,若您有合作意向,请您为我们留言或使用以下方式联系我们, 我们将尽快给你回复,并为您提供真诚的设计服务,谢谢。
    • 电话:028- 86922220 18980695689
    • 商务合作邮箱:631063699@qq.com
    • 合作QQ: 532337155
    • 成都网站设计地址:成都市青羊区锣锅巷31号五金站写字楼6楼

    小谭建站工作室

    成都小谭网站建设公司拥有多年以上互联网从业经验的团队,始终保持务实的风格,以"帮助客户成功"为已任,专注于提供对客户有价值的服务。 我们已为众企业及上市公司提供专业的网站建设服务。我们不只是一家网站建设的网络公司;我们对营销、技术、管理都有自己独特见解,小谭建站采取“创意+综合+营销”一体化的方式为您提供更专业的服务!

    小谭观点

    相对传统的成都网站建设公司而言,小谭是互联网中的网站品牌策划,我们精于企业品牌与互联网相结合的整体战略服务。
    我们始终认为,网站必须注入企业基因,真正使网站成为企业vi的一部分,让整个网站品牌策划体系变的深入而持久。