首页 > 编程知识 正文

javascript 提交form表单,form自动提交表单

时间:2023-05-06 12:33:34 阅读:230639 作者:2044

1.在使用提交表单时,尽量使用form表单,提交时有event参数,好多参数都可通过event.detail.value(此时,表单的各个元素需要添加name属性,否则这里会打印不到)来获取,比较方便,而且可以减少页面变量的使用,当然了,如果有些不能通过event来获取的话,还是得要用变量来储存的;

2.在有些时候可能还要用到formid,在form标签里添加report-submit='true'就可通过event.detail.formid来拿到,formid在模板消息中会用到

3.formid(模板消息,官方很详细,可以看一下)

官网:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/template-message.html

4.代码部分

(1)wxml

<form bindsubmit="formSubmit" bindreset="formReset" report-submit='true'> <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 formType="submit">Submit</button> <button formType="reset">Reset</button> </view></form>

(2)js

formSubmit: function (event) { console.log('event:', event) console.log('form发生了submit事件,携带数据为:', event.detail.value) }, formReset: function () { console.log('form发生了reset事件') }

5.推荐

博客:https://blog.csdn.net/qq_38125123/article/details/74941492

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。