• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

小程序输入框监听事件

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1、html

<view class="coupon-exchange clearfix">
       <view class="code-input">
           <input type="text" placeholder="请输入兑换码" class="input" bindinput="couponExchangeInput" value="{{couponExchangeValue}}" focus="{{couponExchangeInputFocus}}"/>
           <image class="icon-clear" src="{{serverDomain}}miniprogram-img/comm/icon_cancel.png" wx:if="{{iconClearShow}}" bindtap="couponExchangeInputClear"></image>
       </view>
       <view class="btn-exchange {{btnExchangeActive ? 'active' : ''}}" bindtap="couponExchange">兑换</view>
   </view>

2、css

.coupon-exchange{
    width: 702rpx;
    height: 104rpx;
    background: #fff;
    box-shadow: 0 0 4px rgba(153,153,153,0.5);
    margin-top: 20rpx;
    margin-left: auto;
    margin-right: auto;
}
.coupon-exchange .code-input{
    width: 478rpx;
    height: 56rpx;
    background: #eee;
    border-radius: 36rpx;
    margin-left:24rpx;
    margin-top: 24rpx;
    float: left;
    position: relative;
}
.coupon-exchange .code-input .input{
    width: 390rpx;
    height: 36rpx;
    line-height: 36rpx;
    padding: 0rpx 24rpx;
    border: 0;
    background: transparent;
    font-size: 24rpx;
}
.coupon-exchange .code-input .input:focus{
    outline: 0;
}
.coupon-exchange .code-input .icon-clear{
    width: 40rpx;
    height: 40rpx;
    position: absolute;
    top: 8rpx;
    right: 16rpx;
}
.coupon-exchange .btn-exchange{
    float: left;
    width: 134rpx;
    height: 56rpx;
    line-height: 56rpx;
    border-radius: 28rpx;
    background: #bbb;
    color: #fff;
    font-size: 28rpx;
    text-align: center;
    margin-left: 28rpx;
    margin-top: 24rpx;
}
.coupon-exchange .btn-exchange.active{
    background: #C11C1C;
}

3、JS

Page({

  /**
   * 页面的初始数据
   */
  data: {
    couponExchangeValue: '',
    iconClearShow: false,
    btnExchangeActive: false,
  },
couponExchangeInput: function (e) {
    var _this = this;
    var value = e.detail.value;
    var cursor = e.detail.cursor;
    if(value.length > 0){
      this.setData({
        couponExchangeValue: e.detail.value,
        iconClearShow: true,
        btnExchangeActive: true
      });
    }else{
      this.setData({
        couponExchangeValue: e.detail.value,
        iconClearShow: false,
        btnExchangeActive: false
      });
    }
    if(value.length > 0 && value.length == cursor) {
      if(this.strlen(value) > 50){
        value = this.cut_str(value, 50);
        this.setData({
          couponExchangeValue: value,
        })
      }
    }
  },
  couponExchangeInputClear: function (e) {
    this.setData({
      couponExchangeValue: '',
      iconClearShow: false,
      btnExchangeActive: false,
      couponExchangeInputFocus: true,
    });
  },
  couponExchange: function (e) {
    
  },
  strlen: function(str){
    var len = 0;
    for (var i = 0; i < str.length; i++) {
      var c = str.charCodeAt(i);
      if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
        len++;
      }
      else {
        len += 2;
      }
    }
    len = len/2;
    return len;
  },
  cut_str: function (str, len) {
    var char_length = 0;
    for (var i = 0; i < str.length; i++){
      var son_str = str.charAt(i);
      encodeURI(son_str).length > 2 ? char_length += 1 : char_length += 0.5;
      if (char_length >= len){
        var sub_len = char_length == len ? i+1 : i;
        return str.substr(0, sub_len);
        break;
      }
    }
  }
})

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap