问答详情
源自:3-1 todolist功能开发

如何去判断输入框中是空格,是空格得话,不进行li显示。

如何去判断输入框中是空格,是空格得话,不进行li显示。

提问者:喜歡妳時天好暖_z 2019-01-02 17:25

个回答

  • 垣木_v
    2019-01-07 10:06:53
    已采纳

    addValue:function(){
        if(this.inputValue.indexof("")< 0){
            this.arr.push(this.inputValue)
        }
        this.inputValue = ""
    }


  • 前端小白2018
    2019-05-14 20:25:07

    另一个参考答案:


    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,"")   去除两头空格

  • 牧童羊
    2019-02-13 22:14:51

    methods: {

    addValue: function(){

    if(this.msg==""){

    return;

    }

    this.list.push(this.msg),

    this.msg=""

    }

    }


  • 潮汕牛肉丸
    2019-01-03 14:53:11

    if(this.inputValue!="") {    
        this.list.push(this.inputValue)    
        this.inputValue = ""
    }