我已经为我的问题设置了一个 jsFiddle 示例:示例
鉴于此html:
<select id="drop1" data-jsdrop-data="countries"></select>
<select id="drop2" data-jsdrop-data="countries2"></select>
以及以下 javascript:
var countries = {"1" : "Albania", "5" : "Australia","2": "United Arab Emerates"};
var countries2 = {"AL" : "Albania", "AU" : "Australia", "AE" : "United Arab Emerates"};
function loadJSOptions(selector, list) {
$.each(list, function (key, value) {
$(selector).append($("<option></option>").val(key).html(value));
}
});
$(document).ready(function() {
$('select[data-jsdrop-data]').each(function() {
var selector = $(this);
var listname = selector.attr("data-jsdrop-data");
var listvalue = null;
eval("listvalue = " + listname + ";");
loadJSOptions(selector, listvalue);
});
});
谁能向我解释为什么带有字母键的列表是根据输入的顺序列出的,而带有数字键的列表是根据键排序的?如果您查看 jsFiddle 结果,您会看到 drop1 显示 Albania, United Arab Emerates, Australia 而 drop2 显示 Albania, Australia, United Arab Emerates。
谢谢你的帮助。