zoukankan      html  css  js  c++  java
  • 小程序 输入框监听事件

    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;
          }
        }
      }
    })
  • 相关阅读:
    Java实现 蓝桥杯VIP 算法提高 洗牌
    判断一个窗口是否被挂起(发WM_NULL消息,或者调用IsHungAppWindow API进行测试)
    线程天敌TerminateThread与SuspendThread
    Visual C++ 异常(Exception)常见问题 (原文标题:A Visual C++ Exception FAQ)
    阻止屏保运行、显示器和系统待机(使用SystemParametersInfo和SetThreadExecutionState两种办法)
    C语言编译全过程
    MSbuild 教程
    Mac OS X下环境搭建 Sublime Text 2 环境变量配置 开发工具配置Golang (Go语言)
    grunt实用总结
    DDD(领域驱动设计)应对具体业务场景,如何聚焦 Domain Model(领域模型)?
  • 原文地址:https://www.cnblogs.com/ilovexiaoming/p/9909931.html
Copyright © 2011-2022 走看看