我正在尝试按字母顺序对下拉菜单(哈希)进行排序...我尝试了某人发布的排序方法,但我得到的只是每个下拉菜单名称的“未定义”
这是哈希表:
var clientProjectsHash = {};
clientProjectsHash['4'] = {};
clientProjectsHash['4']['name'] = 'Alterna Savins & Credit Union';
clientProjectsHash['4']['projects'] = {};
clientProjectsHash['5'] = {};
clientProjectsHash['5']['name'] = 'BDC';
clientProjectsHash['5']['projects'] = {};
clientProjectsHash['3'] = {};
clientProjectsHash['3']['name'] = 'BELL';
clientProjectsHash['3']['projects'] = {};
clientProjectsHash['6'] = {};
clientProjectsHash['6']['name'] = 'BNC';
clientProjectsHash['6']['projects'] = {};
function getSortedKeys(obj) {
var keys = []; for(var key in obj) keys.push(key);
return keys.sort(function(a,b){return obj[a]-obj[b]});
}
function populateClientSelect(selectedClientId) {
//get the client select
var clientSelect = document.getElementById('clientSelect');
clientProjectsHash = getSortedKeys(clientProjectsHash);
//add the clients
for (clientKey in clientProjectsHash) {
clientSelect.options[clientSelect.options.length] = new Option(clientProjectsHash[clientKey].name, clientKey);
if(selectedClientId == undefined || selectedClientId == 0) {
if(clientKey > 0) {
selectedClientId=clientKey;
}
}
if (clientKey == selectedClientId)
clientSelect.options[clientSelect.options.length-1].selected = true;
}
}
我尝试的一切都不起作用,这让我发疯!
没有排序功能:下拉菜单抓取最低的键 # 这将是 3,然后在列表上输出“BELL”,我实际上希望“Alterna Savins”显示在下拉列表的顶部..