如何将变量作为对象属性传递以显示它

时间:2013-04-29 20:09:04

标签: javascript jquery

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

当我输入一个单词(在这种情况下是运动)时,我希望searchStringInArray()在另一个div中写入object.property。我尝试使用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时它将起作用。

0 个答案:

没有答案