我有一个组用户控件,并且在控件中定义了 javascript。在一个页面中,我有两个这样的控件,它们独立工作。呈现的 javascript 类似于以下内容:
//control 1
var Groups = (function () {
var obj = {};
obj.selectedid = 2;
return obj;
}());
//control 2
var Groups = (function () {
var obj = {};
obj.selectedid = 2; //it is different here, to keep it simple i just hardcoded as '2'
return obj;
}());
谁能告诉我如何访问页面上的第一组变量(控件)。我认为像 Groups[0] 这样的东西会给我结果,但事实并非如此。
感谢您的回复。非常感激。我正在添加更多信息以明确。用户控件包含双列表框,用户可以从 list1 到 list2 添加/删除值。因此,我将所有这些逻辑封装如下。
var Groups = (function () {
var obj = {};
obj.selectedid = 2;
//some local functions for internal operations such as
function moveGroups(source, target){
}
//there are public functions to initialize this control or to add groups as below
// Groups can be added from external page by calling Groups.AddGroups(data);
obj.AddGroups = function(data) {
//refers to local variables and functions and adds data to listboxes
};
return obj;
}());
问题是我在页面上有两个这样的组控件。Group1 包含自己的对偶列表,Group2 也是如此。现在,我需要从页面访问两个对象的 AddGroups 函数,例如 Groups1.AddGroups(data) 或 Groups2.AddGroups(data) 独立工作。
我已经解决了以下解决方案。
var Groups = Groups || []; //Check object already exists
Groups.push(
//kept all the existing code here...
(function () {
var obj = {};
obj.selectedid = 2;
//some local functions for internal operations such as
function moveGroups(source, target){
}
//there are public functions to initialize this control or to add groups as below
// Groups can be added from external page by calling Groups.AddGroups(data);
obj.AddGroups = function(data) {
//refers to local variables and functions and adds data to listboxes
};
return obj;
}())
);
现在在我的页面中,我指的是使用:
Groups[0].AddAvailGroups();
Groups[1].AddAvailGroups();