小程序view选中添加样式

该博客介绍了如何在小程序中创建一个充值选择套餐的页面,替代传统的radio,利用view来实现类似效果。内容包括WXML数据绑定,WXSS样式设计以及JS交互逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

做一个充值选择套餐的页面,没有用radio,直接用view做的效果,用radio传值比较方便。
WXML

<view class='cont'>
        <block wx:for="{{prices}}">
          <view data-select="{{item.select}}" data-price='{{item.price}}' class="{{item.select == priceSelect ? 'active':''}} border" bindtap="choosePrice">
            <view class='conts'>
              <view>{{item.price}}</view>
              <text>{{item.category}}</text>
            </view>
          </view>
        </block>
      </view>

data-price=’{{item.price}}'不需要打印价格出来可去掉。

WXSS

.cont{padding: 10px;padding-right: 0;overflow: hidden;}
.border{width:50%;display: inline-block;text-align: center;margin-bottom: 10px;float: left;}
.conts{background: #fff;border-radius: 5px;border: 1px solid #dedede;margin-right: 10px;padding:10px;}
.conts view{font-size: 24px;border-bottom: 1px dashed #dedede;}
.conts text{font-size: 12px;color: #999;}
.active .conts{color: #33cccc;border: 1px solid #33cccc;}
.active .conts text{color: #33cccc;}

JS

data: {
    prices: [
      { "price": '¥69', "select": 1, "category": '一个月'},
      { "price": '¥699', "select": 2, "category": '一年' },
      { "price": '¥99', "select": 3, "category": '一个月' },
      { "price": '¥999', "select": 4, "category": '一年' },
      { "price": '¥199', "select": 5, "category": '一个月' },
      { "price": '¥1999', "select": 6, "category": '一年' },
      { "price": '¥299', "select": 7, "category": '一个月'},
      { "price": '¥2999', "select": 8, "category": '一年' }
    ],
  },
  choosePrice: function (data) {
    this.setData({//把选中值放入判断值
      priceSelect: data.currentTarget.dataset.select,
    });
    console.log(data.currentTarget.dataset.price);//打印当前价格,不需要可去掉
  },
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值