我想在jQuery中使国家代码自动完成。我以这个code为例。在我的网站中,它运行得非常好,但是这些值在输入前显示为一个列表,而不像站点中的示例。我无法在jsfiddle上运行它,但这里是my code。谢谢!JQuery自动完成与国家
var countries = {
"Argentina (AR)":"AR",
"United States (US)":"US",
"Comoros": "KM",
"Congo (Brazzaville)": "CG",
"Congo, Democratic Republic of the": "CD",
"Cook Islands": "CK",
"Costa Rica": "CR",
"Côte d'Ivoire": "CI",
"Croatia": "HR",
"Cuba": "CU",
"Cyprus": "CY",
"Czech Republic": "CZ",
"Denmark": "DK",
"Djibouti": "DJ",
"Dominica": "DM",
"Dominican Republic": "DO",
};
$("#countryCodes")
// don't navigate away from the field on tab when selecting an item
.bind("keydown", function(event) {
if (event.keyCode === $.ui.keyCode.TAB && $(this).autocomplete("instance").menu.active) {
event.preventDefault();
}
})
.autocomplete({
minLength: 0,
source: function(request, response) {
// delegate back to autocomplete, but extract the last term
response($.ui.autocomplete.filter(
Object.keys(countries), extractLast(request.term)));
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function(event, ui) {
var terms = split(this.value);
// remove the current input
terms.pop();
// add the selected item
terms.push(countries[ui.item.value]);
// add placeholder to get the comma-and-space at the end
terms.push("");
this.value = terms.join(",");
return false;
}
});
谢谢你!我可以解决我的问题,我需要添加一些CSS来解决它。 – jorexe