微信小程序带搜索功能的input框

1.wxml

<view class='list-msg2'>
      <text>车牌号码:</text>
      <text>粤B</text>
      <!-- <input class="list-select" style="width: 45%;" placeholder="输入车牌号码" value="{{licenseNo1}}" bindinput='inputLicense'></input> -->
      <input class="list-select" style="width: 45%;" placeholder="输入车牌号码" type="text" bindinput="bindKeyInput" value="{{licenseNo1}}" data-statu="open" />
    </view>
    <!--mask-->
    <view class="input_screen" bindtap="bindKeyInput" data-statu="close" wx:if="{{showInputStatus}}"></view> 
    <!-- auto-completion  -->
    <scroll-view scroll-y="true" class="scrollview" wx:if="{{showInputStatus}}">
      <view style="margin-top:10px;"></view>
      <view wx:for="{{bindSource}}">
        <view id="{{item.licenseNo}}" data-key="{{item.fourNum}}" class="itemview" bindtap="itemtap" data-statu="close" wx:if="{{showInputStatus}}">
                  {{item.licenseNo}}   
        </view>
      </view>
    </scroll-view>

2. wxss

.list-msg2 {
  height: 60rpx;
  display: flex;
  align-items: center;
  padding: 0 10rpx;
  margin-left: 15px;
  margin-top: 20px;
}
.list-select{
  border: 1px solid #eee;
  width:55%;
  padding-left: 15rpx;
}
.scrollview{
  width: 30%;
  height: 90px;
  background: rgb(162, 210, 238);
  position: relative;
  left: 35%;
  top: -2px;

}

3.wxjs

var app = getApp();
var url = app.globalData.myUrlPath;
Page({

  data: {
    licenseNo:"",
    licenseNo1:"",
    adapterSource: [],
    bindSource: [],
    showInputStatus: false,
  },
  onLoad:function(options){
   
  },
  onUnload:function(){
     var pages =getCurrentPages();//当前页面栈
        if (pages.length >1) {
          var beforePage = pages[pages.length- 2];//获取上一个页面实例对象
          beforePage.refreshData();//触发父页面中的方法
        }
  },
  bindKeyInput: function (e) {
    var currentInputStatu = e.currentTarget.dataset.statu;
    var prefix = e.detail.value//用户实时输入值
    var newSource = []//匹配的结果
    var adapterSource = JSON.parse(this.data.adapterSource);
    var that = this;
    
    if (prefix != "") {
      if(adapterSource.length==0){
        e.detail.value.toUpperCase()
        that.setData({
          licenseNo:"粤B"+e.detail.value.toUpperCase(),
          licenseNo1:e.detail.value.toUpperCase(),
          disabled:false
        });
      }else{
        adapterSource.forEach(function (item, index) {
          if (item.licenseNo.indexOf(prefix.toUpperCase()) != -1) {//返回某个指定的字符串值在字符串中首次出现的位置,如果要检索的字符串值没有出现,则该方法返回 -1
            var no_dict = {
              // "licenseNo":"粤B"+item.licenseNo,
              "licenseNo":item.licenseNo,
              "fourNum":item.fourNum,
            }
            newSource.push(no_dict)
          }else{
            e.detail.value.toUpperCase()
            that.setData({
              licenseNo:"粤B"+e.detail.value.toUpperCase(),
              licenseNo1:e.detail.value.toUpperCase(),
              disabled:false
            });
          }
        })
      }
      
    } else {
      currentInputStatu = "close";
      that.setData(
        {
          isScroll: true,
        }
      );
    }
    if (newSource.length != 0) {
      that.setData({
        bindSource: newSource
      })
    } else {
      that.setData({
        bindSource: []
      })
      currentInputStatu = "close";
      that.setData(
        {
          isScroll: "{{false}}"
        }
      );
    }
    //关闭 
    if (currentInputStatu == "close") {
      that.setData(
        {
          showInputStatus: false,
          isScroll: true
        }
      );
    }
    // 显示 
    if (currentInputStatu == "open") {
      that.setData(
        {
          showInputStatus: true,
          isScroll: "{{false}}"
        }
      );
    }
  },
  //点击选型确定input值
  itemtap: function (e) {
    var currentInputStatu = e.currentTarget.dataset.statu;
    var id = e.currentTarget.dataset.key;
    var license = e.target.id;
    // var result = license.split("粤B")
    this.setData({
      // licenseNo1: result[1],
      licenseNo1: license,
      fourNum1: id,
      bindSource: []
    })
    //关闭 
    if (currentInputStatu == "close") {
      this.setData(
        {
          showInputStatus: false,
          isScroll: true
        }
      );
    }
    // 显示 
    if (currentInputStatu == "open") {
      this.setData(
        {
          showInputStatus: true,
          isScroll: "{{false}}"
        }
      );
    }
  },
})
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值