微信小程序:实现标签与选项卡之间的双向绑定

实现思路:

  1. 给每个标签设置索引 ID,并通过点击事件获取到索引ID。

  2. 选项卡使用swiper标签,通过获取到的索引ID改变swiper的索引从而实现点击标签单向绑定选项卡

  3. swiper绑定change事件,获取到swpier索引并绑定到顶部标签实现选中样式的展示,从而实现双向绑定
    实现代码:

    <view class="kind">
        <view class="kind_button" bindtap="cut" data-id="0"  style="background-color: {{ index == 0 ? '#FDAF04':'#fff'}};color:{{index == 0 ? '#fff':'#000'}};">
            <view>{{kind[0].CHNname}}</view>
            <view>{{kind[0].ENGname}}</view>
        </view>
        <view class="kind_button"bindtap="cut" data-id="1"  style="background-color: {{ index == 1 ? '#FDAF04':'#fff'}};color:{{index == 1 ? '#fff':'#000'}};">
            <view>{{kind[1].CHNname}}</view>
            <view>{{kind[1].ENGname}}</view>
        </view>
        <view class="kind_button"bindtap="cut" data-id="2"  style="background-color: {{ index == 2 ? '#FDAF04':'#fff'}};color:{{index == 2 ? '#fff':'#000'}};">
            <view>{{kind[2].CHNname}}</view>
            <view>{{kind[2].ENGname}}</view>
        </view>
    </view>
    <swiper current="{{index}}" bindchange="changeindex">
            <swiper-item wx:for="{{kind}}">
                <form bindsubmit="submit">
                    <view class="list">
                        <view><text>宠物爱称</text><input name="petname" style="width: 170rpx;"  type="text" placeholder="点击输入"/></view>
                        <view>
                            <text>宠物头像</text>
                        </view>
                        <view><text>品种</text><input name="veriety" type="text"style="width: 170rpx;" placeholder="点击输入  "/></view>
                        <view>
                            <text>宠物出生日期</text>
                            <picker name="date" value="{{selectdate}}" mode="date" bindchange="selectdate">
                                <view>
                                    {{selectdate}}
                                </view>
                            </picker>
                        </view>
                        <view>
                            <text>宠物性别</text>
                            <radio-group name="gender">
                                <radio value="gender_m" checked="true"></radio>
                                <radio value="gender_w"></radio>
                            </radio-group>
                        </view>
                        <view> 
                            <text>是否绝育</text>
                            <radio-group name="JueYU">
                                <radio value="JueYu_t"  checked="true"></radio>
                                <radio value="JueYu_f"></radio>
                            </radio-group>
                        </view>
                    </view>
                    <button class="BaoCun" form-type="submit">点击保存</button>
                </form>
            </swiper-item>
    </swiper>
    
    data: {
        kind:[
            {ENGname:"CAT",CHNname:"小猫"},
            {ENGname:"DOG",CHNname:"小狗"},
            {ENGname:"OTHER",CHNname:"其他"},
        ],
        // 默认·索引
        index:0,
        petimage:""
    },
     cut(e){
        console.log(e);
        this.setData({
            index:e.currentTarget.dataset.id
        })
    },
        // 改变页面索引
    changeindex(e){
        console.log(e);
        this.setData({
            index:e.detail.current
        })
    },

效果预览

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值