嗨,由于某种原因,我无法使用此功能。
function go(type)
{
location=document.category.example.options[document.category.example.selectedIndex].value
}
我想要做的就是嵌入类型变量,每当我尝试执行类似
之类的操作时,go函数都会变为未定义location=document.'+type+'.example.options[document.'+type+'.example.selectedIndex].value
我做错了什么?
答案 0 :(得分:1)
您可以使用document[type].example
答案 1 :(得分:1)
而不是document.'+type+'.example.options[document.'+type+'.example.selectedIndex].value
你应该写:
document[type].example.options[document[type].example.selectedIndex].value
答案 2 :(得分:1)
在执行此类操作之前,您需要了解有关JavaScript语法的更多信息。
以下是您问题的解决方案:
function go(type)
{
location=document[type].example.options[document[type].example.selectedIndex].value;
}
go('category');
它利用了这两个是等价的事实:
a.b === a['b']
但显然在第二部分中,b可以动态替换,因为它是一个字符串。