开发者

Cannot access variable through JavaScript - scope error?

I have some data in a separate .js file similar to this:

data = new Object();
data['cat'] = ['Mr. Whiskers','Wobbles'];
data['dog'] = ['Toothy'];
data['fish'] = ['goldy','roose'];

function getStuff(info)
{
  var stuf开发者_开发百科f = data[info.value];
  return stuff;
}

Now in another html file with a block, I have something like this:

function theDrop(dynamic) {
  alert(getStuff(dynamic));
}

The box says undefined, why?


What are you passing to theDrop? If you want to call the .value then you need to pass the whole object over otherwise you will get undefined

Live Demo

var select = document.getElementById("selectme");

select.onchange = function(){
    theDrop(this);
}

data = new Object();
data['cat'] = ['Mr. Whiskers','Wobbles'];
data['dog'] = ['Toothy'];
data['fish'] = ['goldy','roose'];

function getStuff(info)
{
    var stuff = data[info.value];
    return stuff;
}

function theDrop(dynamic) {
    alert(getStuff(dynamic));
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜