0

我有一种谷歌建议输入框,它在一个数组(列表)中搜索一个单词。我还有一个对象,属性是列表中的那些项目。

当我输入一个单词(在这种情况下为运动)时,我想searchStringInArray()将 object.property 写在另一个 div 中。我已经尝试过definition.list[j]list[j]作为在列表中找到的匹配项。

    var list = ["sports", "hockey"];

var definition = new Object();
    definition.sports = "blablabla";

function searchStringInArray () {
    $('#definition').empty();
    var str=document.getElementById("text").value;
    str = "\\b" + str + "\\b";
    for (var j=0; j<list.length; j++) {
        if(str.length > 4){
        if (list[j].match(str)) {
            alert(definition.list[j]);
            document.getElementById("definition").innerHTML = definition.list[j];
        }
       }
    }
}

我已经尝试了很多事情,但还没有成功。如果我将其更改为 definition.sports,当我在输入字段中输入 sports 时,它将起作用。

4

0 回答 0