cdk
2025-03-24 06e6cf5719a7de9d7919cee2f6fcc3cfc6f9eb9d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<template>
    <uni-base-page>
        <view slot="page">
            <form @submit="formSubmit" @reset="formReset">
                <view class="uni-form-item uni-column form-item">
                    <view class="title">姓名</view>
                    <input class="uni-input" name="nickname" placeholder="请输入姓名" />
                </view>
                <view class="uni-form-item uni-column form-item">
                    <view class="title">性别</view>
                    <radio-group name="gender">
                        <label>
                            <radio value="男" /><text>男</text>
                        </label>
                        <label>
                            <radio value="女" /><text>女</text>
                        </label>
                    </radio-group>
                </view>
                <view class="uni-form-item uni-column form-item">
                    <view class="title">爱好</view>
                    <checkbox-group name="loves">
                        <label>
                            <checkbox value="读书" /><text>读书</text>
                        </label>
                        <label>
                            <checkbox value="写字" /><text>写字</text>
                        </label>
                    </checkbox-group>
                </view>
                <view class="uni-form-item uni-column form-item">
                    <text>城市:</text>
                    <picker class="picker" name="selector" :range="cityList" @change="onCityChange">
                        <text>{{ cityList[cityIndex] }}</text>
                    </picker>
                </view>
                <view class="uni-btn-v">
                    <button type="primary" form-type="submit">提交</button>
                    <button type="default" form-type="reset">重置</button>
                </view>
            </form>
        </view>
    </uni-base-page>
</template>
 
<script>
    import graceChecker from "../../common/graceChecker.js"
 
    export default {
        data() {
            return {
                cityIndex: 0,
                cityList: ['北京', '上海', '广州', '深圳', '杭州'] // 城市列表
            }
        },
        methods: {
            formSubmit: function(e) {
                //控制台查看表单中的数据
                console.log('form发生了submit事件,携带数据为:' + JSON.stringify(e.detail.value));
                //定义表单规则
                var rule = [{
                        name: "nickname",
                        checkType: "string",
                        checkRule: "1,3",
                        errorMsg: "姓名应为1-3个字符"
                    },
                    {
                        name: "gender",
                        checkType: "in",
                        checkRule: "男,女",
                        errorMsg: "请选择性别"
                    },
                    {
                        name: "loves",
                        checkType: "notnull",
                        checkRule: "",
                        errorMsg: "请选择爱好"
                    }
                ];
                //进行表单检查
                var formData = e.detail.value;
                var checkRes = graceChecker.check(formData, rule);
                if (checkRes) {
                    //发送请求
                    this.$post({
                        //请求的路径
                        url: "/demo/sum",
                        //参数
                        data: {
                            x: 1,
                            y: 1
                        },
                        //指定动画中的文字
                        showLoadingTitle: "核对中",
                        //是否显示动画,默认为true
                        showLoading: true,
                    }).then(res => {
                        //返回的结果集
                        console.log(res);
                    })
                } else {
                    uni.showToast({
                        title: graceChecker.error,
                        icon: "none"
                    });
                }
                //此处查看表单的内容
                // uni.showModal({
                //     content: '表单数据内容:' + JSON.stringify(formData),
                //     showCancel: false
                // });
            },
            formReset: function(e) {
                console.log('清空数据')
            },
            onCityChange(event) {
                this.cityIndex = event.mp.detail.value;
            }
        }
    }
</script>
 
<style>
    .uni-form-item .title {
        padding: 20rpx 0;
    }
 
    .form-item {
        margin-bottom: 20rpx;
        --border: 1px solid #000000;
        margin: 10px;
    }
 
    .picker {
        width: 100%;
        box-sizing: border-box;
        padding: 10rpx;
        border: 1px solid #ccc;
    }
</style>