微信小程序weui-form表单的使用

  1. 引入:

app.json: "useExtendedLib":{ "weui": true }

使用页面or组件xxx.json

"usingComponents": {
     "mp-form": "weui-miniprogram/form/form"
  }
  1. wxml构建、
<view class="add-from" mut-bind:tap="empty">
    <mp-form id="addform" ref="addform" rules="{{dynamicRules}}" models="{{formData}}">
        <view class="uni-forms-item" required name="membersOf">
            <label for="membersOf">关系</label>
            <input value="{{formData.membersOf}}" bindinput="handInputChange" type="text" id="membersOf" name="membersOf" placeholder="请输入关系" />
        </view>
        <view class="uni-forms-item" required label="姓名" name="name">
            <label for="name">姓名</label>
            <input value="{{formData.name}}" bindinput="handInputChange" type="nickname" id="name" name="name" placeholder="请输入关系人姓名" />
        </view>
        <view class="uni-forms-item" required label="手机号码" name="phone">
            <label for="phone">手机号码</label>
            <input value="{{formData.phone}}" bindinput="handInputChange" type="text" id="phone" name="phone" placeholder="请输入手机号码" />
        </view>
        <view class="uni-forms-item" required label="身份证号码" name="cardId">
            <label for="cardId">身份证号码</label>
            <input value="{{formData.cardId}}" bindinput="handInputChange" type="idcard" id="cardId" name="cardId" placeholder="请输入身份证号码"/>
        </view>
    </mp-form>
    <button bindtap="submitForm" class="form-submit" type="primary">确定</button>
</view>
  1. js 部分
import {getMembersOfFamily,addMembersOfFamily} from '../../../utils/api'
import {checkIDCard} from '../../../utils/util'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        formData: {
            cardId: '',
            membersOf:'',
            name:'',
            phone:''
        },
        dynamicRules: [
            {
                name: 'cardId',
                rules: [{required: true, errorMessage: '请输入身份证号码'}, {
                    validator: (rule: AnyObject,value: string) => {
                        if(checkIDCard(value)) {
                            return rule.message
                        }
                    }, message: '身份证号码格式不正确'
                }]
            },
            {
                name: 'membersOf',
                rules: [{required: true, errorMessage: '请输入所属关系'}, {maxLength: 10}]
            },
            {
                name: 'name',
                rules: [{required: true, errorMessage: '请输入所属姓名'}, {maxLength: 20}]
            },
            {
                name: 'phone',
                rules: [{required: true, errorMessage: '请输入所属手机号'},{mobile: true, message: '电话格式不对'}]
            }
        ]
    },
    // 简易双向数据绑定
    handInputChange (e:any) {
        this.setData({
            [`formData.${e.target.id}`]: e.detail.value
        })
    },
    submitForm () {
        this.selectComponent('#addform').validate((valid:Boolean, errors:any) => {
            if(valid) {
                // 验证通过
                return
                addMembersOfFamily({
                    ...this.data.formData
                }).then(res => {
                    if((res as AnyObject).code === 200) {
                        wx.showToast({
                            title: '新增成功',
                            icon: 'none'
                        })
                    
                        this.getMembers()
                    } else {
                        wx.showToast({
                            title: (res as AnyObject).msg,
                            icon: 'none'
                        })
                    }
                })
                
            } else {
                const firstError = Object.keys(errors)
                if(firstError.length) {
                    wx.showToast({
                        title: errors[firstError[0]].message,
                        icon: 'none'
                    })
                }
            }
        })
    },

    onShow() {
        this.getMembers()
    }

})
以下是一个简单的微信小程序 weui 实现单选投票的代码示例: 1. 在 app.json 文件中添加页面路径: ``` { "pages": [ "pages/index/index", "pages/vote/vote" ], "window": { "backgroundTextStyle": "light", "navigationBarBackgroundColor": "#fff", "navigationBarTitleText": "WeChat", "navigationBarTextStyle": "black" } } ``` 2. 在 index 页面中添加一个跳转到投票页面的按钮: ``` <view class="container"> <button class="weui-btn" bindtap="goToVote">去投票</button> </view> ``` 3. 在 index 页面的 js 文件中添加 goToVote 函数: ``` Page({ goToVote: function() { wx.navigateTo({ url: '/pages/vote/vote' }) } }) ``` 4. 在 vote 页面中添加一个 radio-group 和若干 radio 控件: ``` <view class="container"> <form bindsubmit="submitVote"> <view class="weui-cells weui-cells_radio"> <label class="weui-cell weui-check__label" for="option1"> <view class="weui-cell__bd">选项1</view> <view class="weui-cell__ft"> <radio class="weui-check" name="options" value="option1" checked="true"></radio> </view> </label> <label class="weui-cell weui-check__label" for="option2"> <view class="weui-cell__bd">选项2</view> <view class="weui-cell__ft"> <radio class="weui-check" name="options" value="option2"></radio> </view> </label> <label class="weui-cell weui-check__label" for="option3"> <view class="weui-cell__bd">选项3</view> <view class="weui-cell__ft"> <radio class="weui-check" name="options" value="option3"></radio> </view> </label> </view> <button class="weui-btn weui-btn_primary" form-type="submit">投票</button> </form> </view> ``` 5. 在 vote 页面的 js 文件中添加 submitVote 函数: ``` Page({ submitVote: function(e) { var option = e.detail.value.options; wx.showModal({ title: '投票结果', content: '您选择了' + option, showCancel: false }) } }) ``` 这样就完成了一个简单的单选投票的微信小程序 weui 实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值