对象字符串中的变量 [英] variable in object string
问题描述
抱歉这个愚蠢的问题,bu6我无法在任何地方找到
答案..
考虑这个无用的功能var的对象..
函数(myParam){
var select1 = document.myForm.mySelectName;
}
我只想让这一行替换为mySelectName。带有var字符串
函数(myParam){
var select1 = document.myForm.myParam;
}
它不起作用,我已尝试使用[] +"" ''''等..
我怎么做这个愚蠢的事情?
sorry for the stupid question, bu6 I haven''t been able to find the
answer anywhere..
consider this useless function which assigns an object to a var..
function(myParam){
var select1 = document.myForm.mySelectName;
}
I simply want this line to subsitute "mySelectName" with a var string
function(myParam){
var select1 = document.myForm.myParam;
}
It doesn''t work, ive tried umpteen variations with []+ "" '''' etc..
How would I do this stupid-simple thing?
推荐答案
bmgz写道:
bmgz wrote:
function(myParam){
var select1 = document.myForm.myParam;
function(myParam){
var select1 = document.myForm.myParam;
var select1 = document.myForm.elements [myParam];
但在大多数情况下,您可以简单地传递对象,例如
表单或选择本身,几乎不需要传递一个字符串
来使用它来查找对象引用。
-
Martin Honnen
http:// JavaScript。 FAQTs.com/
每个对象都继承了哈希(associative array)机制,这就是
对象属性的存储方式。所以:
函数f1(propertyName){
var myVar = someObject [propertyName];
//特定于:
// var myVar = document.formName [propertyName];
}
Each object inherits hash (associative array) mechanics, this is how
objects properties are stored. So:
function f1(propertyName) {
var myVar = someObject[propertyName];
// in the particular:
// var myVar = document.formName[propertyName];
}
bmgz写道:
[...]
bmgz wrote:
[...]
function(myParam){
var select1 = document.myForm.myParam;
}
它没有用,我用[] +""尝试了无数的变化。 ''''等..
我怎么做这个愚蠢的简单的事情?
function(myParam){
var select1 = document.myForm.myParam;
}
It doesn''t work, ive tried umpteen variations with []+ "" '''' etc..
How would I do this stupid-simple thing?
你传递一个对象还是一个字符串?
Mick
Are you passing an object or a string?
Mick
这篇关于对象字符串中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!