0

我可以将 3 条global.getElementById语句连接成一个变量吗?这些是一些代码..

var cmbOrgNodeType = global.getElementById('cmbOrgNodeType').options[global.getElementById('cmbOrgNodeType').selectedIndex].value;    
var cmbOrgNodeSubType = global.getElementById('cmbOrgNodeSubType').options[global.getElementById('cmbOrgNodeSubType').selectedIndex].value;    
var cmbOrgNodeComponentType = global.getElementById('cmbOrgNodeComponentType').options[global.getElementById('cmbOrgNodeComponentType').selectedIndex].value;

我想将这三个连接成单个变量 varTypeID=? 我应该怎么做才能将所有这三个的值存储在单个变量中。?

4

4 回答 4

0

要创建所有值的串联字符串:

var TypeID = cmbOrgNodeType + cmbOrgNodeSubType + cmbOrgNodeComponentType;

除非您仍然需要单独访问,否则我会这样做:

TypeID = { 
  cmbOrgNodeType: null, 
  cmbOrgNodeSubType: null, 
  cmbOrgNodeComponentType: null 
}
for(name in TypeID) {
  TypeID[name] = global.getElementById(name).options[global.getElementById(name).selectedIndex].value;
}

请记住,干燥是件好事:)

于 2012-08-31T13:20:54.660 回答
0

就像把它们放在一起?

var nodes = {
 cmbOrgNodeType: global.getElementById('cmbOrgNodeType').options[global.getElementById('cmbOrgNodeType').selectedIndex].value,
 cmbOrgNodeSubType: global.getElementById('cmbOrgNodeSubType').options[global.getElementById('cmbOrgNodeSubType').selectedIndex].value,
 cmbOrgNodeComponentType: global.getElementById('cmbOrgNodeComponentType').options[global.getElementById('cmbOrgNodeComponentType').selectedIndex].value
}

alert(nodes.cmbOrgNodeType)
alert(nodes.cmbOrgNodeSubType)
alert(nodes.cmbOrgNodeComponentType)
于 2012-08-31T13:21:36.077 回答
0

您可以通过像这样在 javascript 中创建一个对象来做到这一点。

var TypeID={}
TypeID.cmbOrgNodeType =cmbOrgNodeType ;
TypeID.cmbOrgNodeSubType =cmbOrgNodeSubType ;
TypeID.cmbOrgNodeComponentType =cmbOrgNodeComponentType ;

如果需要,您可以像这样访问它。

 TypeID.cmbOrgNodeType
 TypeID.cmbOrgNodeSubType 
 TypeID.cmbOrgNodeComponentType
于 2012-08-31T13:22:05.770 回答
0

您可以从字面上连接字符串:

var type = cmbOrgNodeType + " " + cmbOrgNodeSubType + " " + cmbOrgNodeComponentType;

但是,这将很难从中提取它们的旧值,因此除非您输出它,否则我建议您将它们存储在一个对象中,以便它可以用作一个变量:

var type = {
    "cmbOrgNodeType": global.getElementById('cmbOrgNodeType').options[global.getElementById('cmbOrgNodeType').selectedIndex].value,
    "cmbOrgNodeSubType": global.getElementById('cmbOrgNodeSubType').options[global.getElementById('cmbOrgNodeSubType').selectedIndex].value,
    "cmbOrgNodeComponentType": global.getElementById('cmbOrgNodeComponentType').options[global.getElementById('cmbOrgNodeComponentType').selectedIndex].value
}

(这是对象文字符号

type.cmbOrgNodeType然后您可以使用等访问每个属性。

于 2012-08-31T13:22:12.540 回答