在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Q:我以前写小程序每次获取输入内容,都要写一个方法,觉得十分麻烦,所以写了一个通用的方法。 A:我能想到的原理就是,不同的input所带的data不同,bindinput事件setData不同的data。 <input class="weui-input" bindinput="bindKeyInput" data-inputName='telephone' placeholder="请输入电话号码"/>
// 获取输入框的内容,封装起来,根据data-的值去渲染不同的data bindKeyInput: function (e) {
let inputName = e.currentTarget.dataset.inputname;
this.setData({
[inputName]: e.detail.value
})
},
Q:最近项目有一个需求,就是点击输入框时右边显示清空图标。 A:我的思路是:好好利用bindfocus和bindblur,聚焦函数和非聚焦函数。聚焦时显示清空图标,非聚焦时隐藏清空图标。 // 清空输入框的内容 // 聚焦函数:如果字符串长度为0,则不显示清空图标,否则显示清空图标。 bindKeyFocus: function (e) { let index = e.currentTarget.dataset.index; let fullInputName = `local.is_clear_icon[${index}]`; let inputLength = e.detail.value.length; inputLength == 0 ? this.setData({ [fullInputName]: true }) : this.setData({ [fullInputName]: false }) }, // 非聚焦函数:隐藏清空图标 bindKeyBlur: function (e) { let index = e.currentTarget.dataset.index; let inputName = e.currentTarget.dataset.inputname; let fullInputName = `local.is_clear_icon[${index}]`; let fullInputName2 = `local.${inputName}[0]`; let value = e.detail.value; if (this.data.local.test) { value = [""]; this.setData({ "local.test": false }); } this.setData({ [fullInputName]: true, [fullInputName2]: value }); }, // 点击图标清空 clearInput: function (e) { let inputName = e.currentTarget.dataset.inputname; let fullInputName = `local.${inputName}`; console.log(e.currentTarget.dataset.inputname, fullInputName) let index = e.currentTarget.dataset.index; let clearIcon = `local.focus[${index}]`; this.setData({ [fullInputName]: [""], [clearIcon]: false, "local.test": true }) },
Q:使用wx:for=“{{name}}”去渲染input输入框,而input输入框又修改了name的内容,这样会出现一种情况:用户一输入之后,input便失去聚焦。 A:个人观点:bindinput动态改变name的值,会重新渲染整个html代码。wx:key值设置值不对,改为wx:key="index"即可。 <block wx:for="{{name}}" wx:key="idx" wx:for-index="idx"> <view class="weui-cell weui-cell_input"> <view class="weui-cell__hd wh-card-list-item"> <view class="weui-label wh-required" wx:if="{{idx == 0}}">姓名</view> </view> <view class="weui-cell__bd clear-input-father weui-flex" wx:if="{{idx == 0}}"> <input class="weui-input" data-inputName='name' placeholder="你的姓名" bindinput="bindKeyInput" value='{{item}}' bindfocus="bindKeyFocus" data-index="0" bindblur="bindKeyBlur"/> <icon class='clear-input' type="clear" size="20" hidden='{{is_clear_icon[0]}}' data-inputName='name' data-index="0" catchtap='clearInput'/> </view> <view class="weui-cell__bd clear-input-father weui-flex wh-white-space" wx:else> <view class='wh-white-space' data-index='{{idx}}' data-inputName='name' catchtap='chooseItem'>{{item}}</view> <icon class='clear-input' type="clear" size="20" data-index='{{idx}}' data-inputName='name' catchtap='deteleItem'/> </view> </view> </block> 下面这种就会失去聚焦: <block wx:for="{{telephone}}" wx:key="*this" wx:for-index="idx"> <view class="weui-cell weui-cell_input"> <view class="weui-cell__hd wh-card-list-item"> <view class="weui-label wh-required" wx:if="{{idx == 0}}">手机号</view> </view> <view class="weui-cell__bd clear-input-father weui-flex" wx:if="{{idx == 0}}"> <input class="weui-input" type='number' placeholder="你的手机号" bindinput="bindKeyInput" data-inputName='telephone' value='{{item}}' bindfocus="bindKeyFocus" data-index="1" bindblur="bindKeyBlur" /> <icon class='clear-input' type="clear" size="20" hidden='{{is_clear_icon[1]}}' data-inputName='telephone' data-index="1" catchtap='clearInput'/> </view> <view class="weui-cell__bd clear-input-father weui-flex wh-white-space" wx:else> <view class='wh-white-space' data-index='{{idx}}' data-inputName='telephone' catchtap='chooseItem'>{{item}}</view> <icon class='clear-input' type="clear" size="20" data-index='{{idx}}' data-inputName='telephone' catchtap='deteleItem'/> </view> </view> </block> 这里主要是wx:key的应用:源于http://www.wxappclub.com/topic/536
1:有
|
wk:key |
组件识别 | 渲染情况 | 状态情况 | for效率 |
---|---|---|---|---|
有 | 各组件可识别 | 渲染时仅改变组件顺序 | 保持组件之前原来状态 | 效率高 |
无 | 组件无法识别 | 渲染时重新创建各组件 | 重置组件的初始状态 | 效率低 |
请发表评论