开发者

Dynamically assign values to IDs based upon AJAX content

I have some javascript:

eval('var incomingJSON =' + ajaxObj.responseText);
for (p in incomingJSON.common) {
    document.getElementById(p).value = incomingJSON.common.p;
} 

where ajaxObject.responseText is:

{
    "common": {
        "item1": "1",
        "item2": "2",
        "item3": "3",
        "item4": "4",
        "item5": "5" 
    }
}

The following line works:

document.getElementById(item1).va开发者_如何学Clue = incomingJSON.common.item1;

However, incomingJSON.common.p evaluates to "undefined". The left side of the assign works fine. What's the proper way to access the value in that object given the correct name?


I think you're looking for the bracket notation syntax:

document.getElementById(p).value = incomingJSON.common[p];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜