喜歡妳時天好暖_z
2019-01-02 17:25
如何去判断输入框中是空格,是空格得话,不进行li显示。
addValue:function(){ if(this.inputValue.indexof("")< 0){ this.arr.push(this.inputValue) } this.inputValue = "" }
另一个参考答案:
addValue:
function
(){
if
(
this
.inputValue.match(/\S+/)
){//检查是不是空格,有没有输入内容
this
.arr.push(
this
.inputValue.replace(/^\s+|\s+$/g,"")
);//去除两头空格后,加入数组中
}
this
.inputValue =
""
;
}
注:
/ / 正则表达式必须包含在两个 / 之间,
\S 小写表示匹配空白字符,大写表示匹配非空白字符,这里是大写,只要输入框里不是空白的内容,都有结果
空白字符可以是:
空格符 (space character)
制表符 (tab character)
回车符 (carriage return character)
换行符 (new line character)
垂直换行符 (vertical tab character)
换页符 (form feed character)
match(/\S+/) 这个函数用来获取匹配的结果,如果匹配不到内容会返回 null
^ 用来匹配开头的内容
$ 用来匹配结尾的内容
replace(/^\s+|\s+$/g,"") 去除两头空格
methods: {
addValue: function(){
if(this.msg==""){
return;
}
this.list.push(this.msg),
this.msg=""
}
}
if(this.inputValue!="") { this.list.push(this.inputValue) this.inputValue = "" }
vue2.5入门
146183 学习 · 655 问题
相似问题