带有重音折叠和链接的自动完成 jquery
Autocomplete jquery with accent folding and links
您好,我的页面上有这个自动完成 jquery 脚本:
var source = [ { value: "http://www.bata.com",
label: "Baťa"
},
{ value: "http://www.bata.com",
label: "Bata"
},
];
$(document).ready(function() {
$("input#autocomplete").autocomplete({
source: source,
select: function( event, ui ) {
window.location.href = ui.item.value;
}
});
});
<input id="autocomplete" />
一切都可以,但我想在此代码中添加 accent folding,如下所示:
https://jqueryui.com/autocomplete/#folding
因为,当我在输入框中写:
"bata" 自动完成显示:Bata
"baťa" 自动完成显示:Baťa
我想要这个:当我在输入框里写的时候:
"bata" 自动完成显示:Bata、Baťa
谢谢
在 link 中,单击 查看源代码 显示用法。
以下是它的工作原理:-
var source = [ { value: "http://www.bata.com",
label: "Baťa"
},
{ value: "http://www.bata.com",
label: "Bata"
},
];
var accentMap = {
"á": "a",
"ö": "o",
"ť": "t"
// ADD MORE HERE
};
var normalize = function( term ) {
var ret = "";
for ( var i = 0; i < term.length; i++ ) {
ret += accentMap[ term.charAt(i) ] || term.charAt(i);
}
return ret;
}
$("input#autocomplete").autocomplete({
source: function( request, response ) {
var matcher = new RegExp( $.ui.autocomplete.escapeRegex( request.term ), "i" );
response($.grep(source, function(value) {
return matcher.test(value.label) || matcher.test(normalize(value.label));
})
);
},
select: function( event, ui ) {
window.location.href = ui.item.value;
}
});
<link href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<input id="autocomplete" />
您好,我的页面上有这个自动完成 jquery 脚本:
var source = [ { value: "http://www.bata.com",
label: "Baťa"
},
{ value: "http://www.bata.com",
label: "Bata"
},
];
$(document).ready(function() {
$("input#autocomplete").autocomplete({
source: source,
select: function( event, ui ) {
window.location.href = ui.item.value;
}
});
});
<input id="autocomplete" />
一切都可以,但我想在此代码中添加 accent folding,如下所示: https://jqueryui.com/autocomplete/#folding
因为,当我在输入框中写:
"bata" 自动完成显示:Bata
"baťa" 自动完成显示:Baťa
我想要这个:当我在输入框里写的时候:
"bata" 自动完成显示:Bata、Baťa
谢谢
在 link 中,单击 查看源代码 显示用法。
以下是它的工作原理:-
var source = [ { value: "http://www.bata.com",
label: "Baťa"
},
{ value: "http://www.bata.com",
label: "Bata"
},
];
var accentMap = {
"á": "a",
"ö": "o",
"ť": "t"
// ADD MORE HERE
};
var normalize = function( term ) {
var ret = "";
for ( var i = 0; i < term.length; i++ ) {
ret += accentMap[ term.charAt(i) ] || term.charAt(i);
}
return ret;
}
$("input#autocomplete").autocomplete({
source: function( request, response ) {
var matcher = new RegExp( $.ui.autocomplete.escapeRegex( request.term ), "i" );
response($.grep(source, function(value) {
return matcher.test(value.label) || matcher.test(normalize(value.label));
})
);
},
select: function( event, ui ) {
window.location.href = ui.item.value;
}
});
<link href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<input id="autocomplete" />