gpt4 book ai didi

javascript - JQuery 自动完成国家/地区

转载 作者:行者123 更新时间:2023-12-03 07:45:26 24 4
gpt4 key购买 nike

我正在尝试在 jquery 中使用国家/地区代码进行自动完成。我正在使用这个code例如。在我的网站中,它运行得很好,但值显示为输入前面的列表,而不像网站中的示例。我无法在 jsfiddle 上运行它,但这里是 my code 。谢谢你!

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

最佳答案

fiddle 中缺少exactLast 函数。我现在添加了,现在它可以在 Fiddle 中使用了

function extractLast( term ) {
return split( term ).pop();
}

https://jsfiddle.net/px2dmd1o/1/

检查并让我知道您的问题是什么

关于javascript - JQuery 自动完成国家/地区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35227276/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com