indexof的使用

在写登录页面,输入账号时,为了防止用户输入“@”符号时,下拉框会显示两个@符号,所以用dealtext:function(){return this.text.slice(0,this.text.indexOf('@'))}处理

但是却出现了这种现象

原因是indexof,indexof返回整数值,找到string对象内字符串的开始位置,若没有则返回-1。

解决方法:加一个判断

if(this.text.includes("@")){
return this.text.slice(0,this.text.indexOf('@'))
}else{
return this.text;
}

猜你喜欢

转载自www.cnblogs.com/wkkke/p/9826599.html
今日推荐