admin管理员组文章数量:1022964
var test1;
$(document).ready(function () {
test1 = $("#test1ID").jQueryPlugin();
});
var test2;
$(document).ready(function () {
test2 = $("#test2ID").jQueryPlugin();
});
...
This is done so we could just do test1.foo()
... foo is a function inside the jQueryPlugin that is accessible using test1.foo()
syntax;
So we have an array of strings which contains (test1, test2, ...) and we need to access foo() while on the loop:
for(i=0; i < theArrayOfStrings.length; i++){
theArrayOfStrings[i].foo();
//so here is the problem... we can't do test1.foo(), test2.foo() ... =(
}
Any idea on how to call function foo() while on the loop? Or can we convert a string value to a variable in javascript?
var test1;
$(document).ready(function () {
test1 = $("#test1ID").jQueryPlugin();
});
var test2;
$(document).ready(function () {
test2 = $("#test2ID").jQueryPlugin();
});
...
This is done so we could just do test1.foo()
... foo is a function inside the jQueryPlugin that is accessible using test1.foo()
syntax;
So we have an array of strings which contains (test1, test2, ...) and we need to access foo() while on the loop:
for(i=0; i < theArrayOfStrings.length; i++){
theArrayOfStrings[i].foo();
//so here is the problem... we can't do test1.foo(), test2.foo() ... =(
}
Any idea on how to call function foo() while on the loop? Or can we convert a string value to a variable in javascript?
Share Improve this question edited Dec 12, 2009 at 12:41 Jronny asked Dec 12, 2009 at 12:33 JronnyJronny 2,3044 gold badges32 silver badges41 bronze badges5 Answers
Reset to default 4It might be worth creating an object to hold all your "tests":
var tests = {};
$(document).ready(function () {
tests.test1 = $("#test1ID").jQueryPlugin();
tests.test2 = $("#test2ID").jQueryPlugin();
});
for(i=0; i < theArrayOfStrings.length; i++){
tests[theArrayOfStrings[i]].foo();
}
eval() function is used to evaluate script in a string variable. For example :
var test1;
eval("test1=" + theArrayOfStrings[i]);
test1.foo();
But take a lok at this question before use When is JavaScript’s eval() not evil?
If test1
is a global variable you can access it by name through the window
object:
window[theArrayOfStrings[0]].foo(); // test1();
If it's not, eval
is the only way, but I'd strongly advise avoiding eval
in all circumstances. Using a lookup as in J-P's answer (+1) is much much more appropriate than selecting variable names.
Have you tried
$('#' + theArrayOfStrings[i]).foo();
Have a look at API/1.3/Selectors
var test = [], n = 5;
$(document).ready(function () {
for(var i=0; i < n; i++)
test.push($("#test"+i+"ID").jQueryPlugin());
});
// the values in test won't be accessible before the document is loaded.
var test1;
$(document).ready(function () {
test1 = $("#test1ID").jQueryPlugin();
});
var test2;
$(document).ready(function () {
test2 = $("#test2ID").jQueryPlugin();
});
...
This is done so we could just do test1.foo()
... foo is a function inside the jQueryPlugin that is accessible using test1.foo()
syntax;
So we have an array of strings which contains (test1, test2, ...) and we need to access foo() while on the loop:
for(i=0; i < theArrayOfStrings.length; i++){
theArrayOfStrings[i].foo();
//so here is the problem... we can't do test1.foo(), test2.foo() ... =(
}
Any idea on how to call function foo() while on the loop? Or can we convert a string value to a variable in javascript?
var test1;
$(document).ready(function () {
test1 = $("#test1ID").jQueryPlugin();
});
var test2;
$(document).ready(function () {
test2 = $("#test2ID").jQueryPlugin();
});
...
This is done so we could just do test1.foo()
... foo is a function inside the jQueryPlugin that is accessible using test1.foo()
syntax;
So we have an array of strings which contains (test1, test2, ...) and we need to access foo() while on the loop:
for(i=0; i < theArrayOfStrings.length; i++){
theArrayOfStrings[i].foo();
//so here is the problem... we can't do test1.foo(), test2.foo() ... =(
}
Any idea on how to call function foo() while on the loop? Or can we convert a string value to a variable in javascript?
Share Improve this question edited Dec 12, 2009 at 12:41 Jronny asked Dec 12, 2009 at 12:33 JronnyJronny 2,3044 gold badges32 silver badges41 bronze badges5 Answers
Reset to default 4It might be worth creating an object to hold all your "tests":
var tests = {};
$(document).ready(function () {
tests.test1 = $("#test1ID").jQueryPlugin();
tests.test2 = $("#test2ID").jQueryPlugin();
});
for(i=0; i < theArrayOfStrings.length; i++){
tests[theArrayOfStrings[i]].foo();
}
eval() function is used to evaluate script in a string variable. For example :
var test1;
eval("test1=" + theArrayOfStrings[i]);
test1.foo();
But take a lok at this question before use When is JavaScript’s eval() not evil?
If test1
is a global variable you can access it by name through the window
object:
window[theArrayOfStrings[0]].foo(); // test1();
If it's not, eval
is the only way, but I'd strongly advise avoiding eval
in all circumstances. Using a lookup as in J-P's answer (+1) is much much more appropriate than selecting variable names.
Have you tried
$('#' + theArrayOfStrings[i]).foo();
Have a look at API/1.3/Selectors
var test = [], n = 5;
$(document).ready(function () {
for(var i=0; i < n; i++)
test.push($("#test"+i+"ID").jQueryPlugin());
});
// the values in test won't be accessible before the document is loaded.
本文标签: How to convert a string value to a variable in javascriptStack Overflow
版权声明:本文标题:How to convert a string value to a variable in javascript? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745579741a2157237.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论