[英]Dynamic jQuery variable names
I would like to take the value of an li ID attribute (which will be a userID), and use it as part of a string that I will eventually use as part of a variable name. 我想获取li ID属性的值(它将是一个userID),并将其用作字符串的一部分,我最终将其用作变量名称的一部分。 I will use this variable name to create an array with.
我将使用此变量名来创建一个数组。
I understand the basics, but can't seem to find the right combination of jQuery/javascript to make this magic happen. 我理解基础知识,但似乎无法找到正确的jQuery / javascript组合来实现这种魔力。
jQuery('#user-list li').click(function() {
var userID = jQuery(this).attr("id");
// i want to add the word array to the end of userID
var theVariableName = userID + "Array";
// I want to use this variable to create an array
var theVariableName = new Array();
// I want to continue to use the name throughout my document
theVariableName.push({startTime: 7, endTime: 10});
alert(theVariableName[0].startTime);
});
Use an Object to hold the various user arrays: 使用Object来保存各种用户数组:
window.userData = {};
$(...).click(function() {
// ...
window.userData[userID] = [];
window.userData[userID].push({startTime:7, endTime:10});
alert(window.userData[userID][0].startTime);
}
You might not want to store the userData
object in the global namespace though; 您可能不希望将
userData
对象存储在全局命名空间中; to prevent accidental name conflicts, you should at least put it in your own namespace. 为了防止意外的名称冲突,您至少应该将它放在您自己的命名空间中。
You can store variables in the global window
object: 您可以在全局
window
对象中存储变量:
jQuery('#user-list li').click(function() {
var userID = jQuery(this).attr("id");
// i want to add the word array to the end of userID
var theVariableName = userID + "Array";
// I want to use this variable to create an array
window[theVariableName] = new Array();
// I want to continue to use the name throughout my document
window[theVariableName].push({startTime: 7, endTime: 10});
alert(window[theVariableName][0].startTime);
});
In fact every var x
declared variable x
that has not been declared in a closure will reside in the global object. 实际上,每个
var x
声明的变量x
都未在闭包中声明,它将驻留在全局对象中。 However, I recommend you to use another global object, eg userStorageObject
or something similar: 但是,我建议您使用另一个全局对象,例如
userStorageObject
或类似的东西:
var userStorageObject = {};
jQuery('#user-list li').click(function() {
var userID = jQuery(this).attr("id");
// i want to add the word array to the end of userID
var theVariableName = userID + "Array";
// I want to use this variable to create an array
userStorageObject[theVariableName] = new Array();
// I want to continue to use the name throughout my document
userStorageObject[theVariableName].push({startTime: 7, endTime: 10});
alert(userStorageObject[theVariableName][0].startTime);
});
it works here: http://jsfiddle.net/bingjie2680/NnnRk/ 它在这里工作: http : //jsfiddle.net/bingjie2680/NnnRk/
You can do that like this.. 你可以这样做..
var variable = "Array";
window[id+variable] = "value";
Try eval
: 试试
eval
:
var theVariableName = userID + "Array";
eval(theVariableName+"= new Array()");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.