如何在JavaScript中检查输入是否有字符串

如何在JavaScript中检查输入是否有字符串,javascript,jquery,html,events,html-input,Javascript,Jquery,Html,Events,Html Input,我在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)

我在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”,那么它将从表1加载autocomplete,否则它将从表2加载。如何检查输入是否包含该字符串?提前感谢。

您应该使用
includes
方法

var inputValue=$("#myinput").val();
if(inputValue.toLowerCase().includes("where")){
    //rest of code
}
if(inputValue.indexOf("where")!==-1){
    //rest of code
}
if(inputValue.search(/where/i)!==-1){
    //rest of code
}
另一种方法是使用
indexOf
方法

var inputValue=$("#myinput").val();
if(inputValue.toLowerCase().includes("where")){
    //rest of code
}
if(inputValue.indexOf("where")!==-1){
    //rest of code
}
if(inputValue.search(/where/i)!==-1){
    //rest of code
}
如果您想使用
regex
实现此目标,可以使用
search
方法

var inputValue=$("#myinput").val();
if(inputValue.toLowerCase().includes("where")){
    //rest of code
}
if(inputValue.indexOf("where")!==-1){
    //rest of code
}
if(inputValue.search(/where/i)!==-1){
    //rest of code
}
inputValue=“awherea”;

console.log(inputValue.search(/where/)
如果您想要最强大的浏览器支持,请使用


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

my_inp = $("#myinput").val();
if (my_inp.indexOf('where') > -1) {
   console.log('yes');
}
尝试使用方法返回true或false

而且还有一些比较好的方法


  • console.log(“大家好”。匹配(“你好”)?真:假)
    您可以使用
    typeof
    检查输入框中的值?@mohidenibnhammed yes。检查我的答案。。您可以使用indexof来查找它的TR。搜索也适用我认为
    indexof
    更好,因为IE不支持
    includes
    也可以使用
    match