uni-app input组件在手机端输入赋值时无法及时更新数据
uni-app input组件在手机端输入赋值时无法及时更新数据
| 开发环境 | 版本号 | 项目创建方式 |
|---|---|---|
| Windows | 10 | HBuilderX |
示例代码:
<view class="itemBox">
<input type="text" :value="account" @blur="getAccount" @input="accountStatus($event, 'account')" placeholder="请输入手机">
</view>
<view class="itemBox mb0">
<input :password="true" :value="password" @blur="getPassword" @input="accountStatus($event, 'password')" placeholder="请输入登录密码">
</view>
操作步骤:
// 校验账号或电话是否为空
accountStatus(e, flag) {
console.log(e);
if(flag == 'account'){
let accountText = "";
setTimeout(() =>{this.getValue(flag,e.detail.value);}, 10);
// this.getValue(flag,e.detail.value);
if (e.detail.value) {
this.isAccount = true;
} else {
this.isAccount = false;
}
if (this.isAccount && this.isPassword) {
this.disabledLogin = false;
this.customStyle.backgroundColor = "#0883FD !important";
} else if (!this.isAccount || !this.isPassword) {
this.disabledLogin = true;
this.customStyle.backgroundColor = "rgba(8,131,253, 0.3) !important";
}
} else if(flag == 'password'){
let passwordText = "";
setTimeout(() =>{passwordText = e.detail.value}, 0);
this.getValue(flag,e.detail.value);
if (e.detail.value) {
this.isPassword = true;
} else {
this.isPassword = false;
}
if (this.isAccount && this.isPassword) {
this.disabledLogin = false;
this.customStyle.backgroundColor = "#0883FD !important";
} else if (!this.isAccount || !this.isPassword) {
this.disabledLogin = true;
this.customStyle.backgroundColor = "rgba(8,131,253, 0.3) !important";
}
}
},
更多关于uni-app input组件在手机端输入赋值时无法及时更新数据的实战教程也可以访问 https://www.itying.com/category-93-b0.html
7 回复
这是这个组件就有问题,而且很久了习惯了,因为你在更新那个value值,删除也是在实时更新这个值就会跟不上视图的反应速度,删除速度很快的,所以这个更新这个值的时候太慢跟不上,就会有删除的幻影。反正我是直接定义两个值,去交互使用。
更多关于uni-app input组件在手机端输入赋值时无法及时更新数据的实战教程也可以访问 https://www.itying.com/category-93-b0.html
可以贴下代码?
更新要用this.$nextTick,哈哈,不过会有闪烁问题,input没有html5的好用
从代码分析来看,数据更新延迟问题主要出现在accountStatus方法中的异步处理逻辑上。
在account分支中使用了setTimeout延迟10ms执行getValue,这会破坏数据的同步更新。而在password分支中虽然也有setTimeout,但passwordText变量赋值后并未实际使用,同时立即执行了getValue,逻辑不一致。
建议采用以下优化方案:
- 统一使用v-model双向绑定
<input type="text" v-model="account" @input="accountStatus($event, 'account')">
<input :password="true" v-model="password" @input="accountStatus($event, 'password')">
- 简化accountStatus方法
accountStatus(e, flag) {
const value = e.detail.value;
// 直接更新对应状态
if(flag === 'account') {
this.isAccount = !!value;
} else if(flag === 'password') {
this.isPassword = !!value;
}
// 统一处理登录按钮状态
this.updateLoginButtonState();
},
updateLoginButtonState() {
this.disabledLogin = !(this.isAccount && this.isPassword);
this.customStyle.backgroundColor = this.disabledLogin
? "rgba(8,131,253, 0.3) !important"
: "#0883FD !important";
}


