function inputEvent() {
const str = this.inputValue.split(""); // 把input的value分隔成数组
const newValue = []; // 新生成的value
let leng = 0; // 新value的长度
for (let i = 0; i < str.length; i++) {
if (/^[\u4e00-\u9fa5]+$/i.test(str[i])) {
// 判断是否为汉字,汉字算为3个字符
leng += 3;
} else {
leng += 1;
}
if (leng > 10) { // 长度超过10位则不再继续push
break;
}
newValue.push(str[i]);
}
this.inputValue = newValue.join(""); // newValue转为字符串
}
input的value任意输入不超过10个字符
最新推荐文章于 2024-01-31 16:21:35 发布