ios微信小程序中 input 输入框问题,输入的光标中的内容自动清空

23 篇文章 0 订阅
13 篇文章 0 订阅

ios微信小程序中 input 输入框问题,输入的光标中的内容自动清空

问题描述

1.我在页面上有个倒计时的功能,每一秒都在更新时间,更新时间显示的值(view一直变)
2.我调用input输入框,准备输入内容,每次倒计时秒数变得时候,光标的内容自动全部清空

具体场景描述请见下图:

image

解决方案

这个问题是 mpvue 处理定时器和 input 输入框在ios上的兼容问题;问题连接附上:

github issuse 直达车

github 上有道友出了几个方案,但是都没有解决我的项目场景;最后捋了捋思路:既然 input 绑定数据会和定时器冲突,那就可以不绑定;代码如下(简写思路):

<template>
    <figure class="main-input-box-textarea-inner" v-if="textareaShow">
        <section class="area-content">
              <input class="main-input-box-textarea"
                     v-if="textareaShow"
                     type="text"
                     @input="inputFunc"
                     maxlength="500"
                     @focus="scrollToBottom()" cursor-spacing="5"/>
              <div class="main-input-box-textarea" v-if="!textareaShow"></div>
        </section>
        <button class="main-input-box-send" :class="{'on':sendTextContent.length}" @click="sendMessage"
                type="submit" formType="submit">发送
        </button>
    </figure>
</template>
<script type="text/ecmascript-6">
export default {
    methods: {
        inputFunc (e) {
            console.log(e.target.value)
            this.sendTextContent = e.target.value
        },
        sendMessage() {
            if (this.sendTextContent.trim().length === 0) {
              return false;
            } else {
              this.textareaShow = false;//这里是重点
              if (!this.clickFlag) return false;
              this.clickFlag = false;
              console.log(this.sendTextContent.trim().substring(0,20));
              dataLog.createTrack({
                actionId: 14202,
                keyword: this.sendTextContent.trim()
              });
              imBaseMethods.sendMessage({
                target: this.targetData.account,
                sendContent: this.sendTextContent.trim(),
                custom: JSON.stringify({
                  cType: "1",
                  cId: this.doctorCustomerId,
                  mType: "0",
                  conId: this.orderSourceId
                }),
                needPushNick: false,
                pushContent: `患者${this.patientName ? this.patientName : ""}:${this.sendTextContent.trim().substring(0,20)}`,
                pushPayload: JSON.stringify({
                  account: "0_" + this.caseId,
                  type: "1"
                }),
              }).then(obj => {
                this.textareaShow = true; //这里是重点
                this.setFocus = true;
                this.sendMessageSuccess(obj);
              }).catch((err, obj) => {
                this.sendFail(err, obj)
              });
            }
          },
    }
}
</script>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序开发,如果在iOS手机上遇到了键盘弹起时会遮挡上面内容问题,可以通过以下方法解决。 首先,我们需要获取键盘的高度。在小程序,可以使用wx.getSystemInfoSync()方法来获取系统信息,其包括键盘的高度。 接下来,我们需要监听键盘的弹起和隐藏事件。可以通过监听页面的focus和blur事件来实现。当input框被选时(focus事件),我们可以将页面整体上移一个键盘高度的距离,以保证输入框不被键盘遮挡。当input框失去焦点时(blur事件),我们将页面恢复到原始位置。 具体实现时,可以在页面的onLoad生命周期函数调用wx.getSystemInfoSync()方法获取系统信息,并存储键盘的高度。然后在页面的focus和blur事件分别设置页面的上下边距,以达到上移和恢复的效果。 示例代码如下: ``` // 在页面的onLoad生命周期函数获取系统信息 onLoad: function() { var that = this; wx.getSystemInfoSync({ success: function(res) { that.setData({ keyboardHeight: res.windowHeight * 0.75 // 假设键盘高度占屏幕高度的3/4 }); } }); }, // input框获取焦点时的事件处理函数 onFocus: function() { this.setData({ marginTop: -this.data.keyboardHeight }); }, // input框失去焦点时的事件处理函数 onBlur: function() { this.setData({ marginTop: 0 }); } ``` 需要注意的是,根据不同手机型号和键盘设置的不同,键盘的高度可能会有所差异,因此上述代码获取到的键盘高度是一个近似值,可以根据实际情况进行调整。 以上是解决微信小程序开发iOS手机键盘弹起时会遮挡上面内容的一种方法,希望能对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值