【问题标题】:How to check an input has a string in JavaScript如何在 JavaScript 中检查输入是否有字符串
【发布时间】:2017-11-03 12:09:13
【问题描述】:

我在 HTML 输入中有此代码用于自动完成:

$("#myinput")
    .bind("keydown", function(event) {
        // don't navigate away from the field on tab when selecting an item
        if (event.keyCode === $.ui.keyCode.TAB 
                                   && $(this).data("autocomplete").menu.active) {
            event.preventDefault();
        }
    })
    .autocomplete({
        minLength: 0,
        source: function(request, response) {
            var results = [],
                selectionStart = this.element[0].selectionStart
                term = extractLast(request.term.substring(0, selectionStart));

                if (term.length > 0) {
                    console.log(term);
                if(/*input has string "where"*/)
                results = $.ui.autocomplete.filter(table1, term);
                else
                results = $.ui.autocomplete.filter(table2, term);   
            }
            response(results);
        },
        focus: function() {
            return false; // prevent value inserted on focus
        },
        select: function(event, ui) {
            var terms = split(this.value.substring(0, this.selectionStart));
            terms.pop();  // remove the current input
            terms.push(ui.item.value);        // add the selected item
            this.value = 
                $.trim(terms.join(" ") + this.value.substring(this.selectionStart)) + " ";
            return false;
        }
    });

我想要做的是,如果输入在某处有字符串“where”,那么它将从 table1 加载自动完成,否则它将从 table2 加载。如何检查输入是否有该字符串?提前致谢。

【问题讨论】:

标签: javascript jquery html events html-input


【解决方案1】:

你应该使用includes方法。

var inputValue=$("#myinput").val();
if(inputValue.toLowerCase().includes("where")){
    //rest of code
}

另一种方法是使用indexOf 方法。

if(inputValue.indexOf("where")!==-1){
    //rest of code
}

如果你想使用regex来实现这个目标,你可以使用search方法。

if(inputValue.search(/where/i)!==-1){
    //rest of code
}

inputValue="awherea";
console.log(inputValue.search(/where/))

【讨论】:

  • str.search 也适用
  • 我觉得indexOf更好,因为IE不支持includes
  • 也可以使用match
【解决方案2】:

如果您想要最强大的浏览器支持,请使用String#indexOf

if(this.value.indexOf('where') > -1) {
   //doSomething
}

【讨论】:

    【解决方案3】:

    您可以获取您的文本值并使用 indexof 来查找它。

    my_inp = $("#myinput").val();
    if (my_inp.indexOf('where') > -1) {
       console.log('yes');
    }
    

    【讨论】:

      【解决方案4】:

      尝试使用string#match 方法,使用ternary operator 返回真或假

      还有一些更多的方法

      1. string.indexOf()
      2. string.includes()

      console.log('hello everyone'.match("hello") ? true : false)

      对于 jquery,您可以使用 contains() 方法

      console.log($('p').is(':contains("hi")')) 
      console.log($('p').is(':contains("22")')) // not contains
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <p>hello hi</p>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-08-12
        • 2011-07-22
        • 1970-01-01
        相关资源
        最近更新 更多