解决element 里面的el-input 汉字算两个字符
template
<el-form-item label="名称:" prop="title"> <el-input v-model="form.title" clearable type="textarea" :maxlength="maxLength" placeholder="请输入项目名称(一个汉字等于两个字符)" class="inputDesc" @input="checkLength" ></el-input> <span class="input-count">{{ textLength }}/{{ maxCharLength }}</span> </el-form-item>
script
method:{
checkLength(e) { console.log(e); // js字数与数据库字符数不一样 let b = 0; // 输入的字符数 for (let i = 0, length = e.length; i < length; i++) { let c = e.charAt(i); if (/^[\u0000-\u00ff]$/.test(c)) { b++; } else { b += 2; } if (b > this.maxCharLength) { this.form.title = e.substr(0, i); if (/^[\u0000-\u00ff]$/.test(c)) { b--; } else { b -= 2; } break; } } this.textLength = b; },
}
标签:el,字符,element,length,let,input From: https://www.cnblogs.com/overspread/p/16911828.html注意:请把内容中的变量替换成自己的变量