2012-08-31 58 views
0

我可以連接3 global.getElementById語句轉換成一個單一的variable.these一些代碼..串聯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; 

我想這三個連接成一個變量var TypeID=? 我應該怎麼將所有這三個值存儲在單個變量中。

回答

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; 
} 

請記住,這是很好的是DRY :)

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) 
0

你可以通過在javascript中創建一個對象來做到這一點。

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

如果需要,您可以像這樣訪問它。

TypeID.cmbOrgNodeType 
TypeID.cmbOrgNodeSubType 
TypeID.cmbOrgNodeComponentType 
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 
} 

(這是object-literal notation

然後您可以使用type.cmbOrgNodeType等訪問每個屬性。