uniapp表单数据提交及获取方法

2021-01-09   阅读:373   分类:前端    标签: uni-app

可以使用form将组件内的用户输入的<switch> <input> <checkbox> <slider> <radio> <picker> 提交。

例子:

<template>
    <view>
        <view>
            <form @submit="formSubmit" @reset="formReset">
                <view class="uni-form-item uni-column">
                    <view>switch</view>
                    <view>
                        <switch name="switch" />
                    </view>
                </view>
                <view class="uni-form-item uni-column">
                    <view>radio</view>
                    <radio-group name="radio">
                        <label>
                            <radio value="radio1" /><text>选项一</text>
                        </label>
                        <label>
                            <radio value="radio2" /><text>选项二</text>
                        </label>
                    </radio-group>
                </view>
                <view class="uni-form-item uni-column">
                    <view>checkbox</view>
                    <checkbox-group name="checkbox">
                        <label>
                            <checkbox value="checkbox1" /><text>选项一</text>
                        </label>
                        <label>
                            <checkbox value="checkbox2" /><text>选项二</text>
                        </label>
                    </checkbox-group>
                </view>
                <view class="uni-form-item uni-column">
                    <view>slider</view>
                    <slider value="50" name="slider" show-value></slider>
                </view>
                <view class="uni-form-item uni-column">
                    <view>input</view>
                    <input name="input" placeholder="这是一个输入框" />
                </view>
                <view>
                    <button form-type="submit">Submit</button>
                    <button type="default" form-type="reset">Reset</button>
                </view>
            </form>
        </view>
    </view>
</template>
<script>
    export default {
        data() {
            return {
            }
        },
        methods: {
            formSubmit: function(e) {
                console.log('form发生了submit事件,携带数据为:' + JSON.stringify(e.detail.value))
                var formdata = e.detail.value
                uni.showModal({
                    content: '表单数据内容:' + JSON.stringify(formdata),
                    showCancel: false
                });
            },
            formReset: function(e) {
                console.log('清空数据')
            }
        }
    }
</script>
 
<style>
    .uni-form-item .title {
        padding: 20rpx 0;
    }
</style>


【腾讯云】云产品限时秒杀,爆款1核2G云服务器,首年99元

‘简忆博客’微信公众号 扫码关注‘简忆博客’微信公众号,获取最新文章动态
转载:请说明文章出处“来源简忆博客”。http://www.tpxhm.com/fdetail/576.html

×
觉得文章有用就打赏一下文章作者
微信扫一扫打赏 微信扫一扫打赏
支付宝扫一扫打赏 支付宝扫一扫打赏

文章评论(0)

登录
简忆博客壁纸 头像

简忆博客
仕而优则学,学而优则仕。

置顶推荐

打赏本站

如果你觉得本站很棒,可以通过扫码支付打赏哦!
微信扫码:你说多少就多少~
微信扫码
支付宝扫码:你说多少就多少~
支付宝扫码
×