jQuery completamento automatico con caratteri speciali (vale a dire ö, a, e, o ß)
-
25-09-2019 - |
Domanda
Come posso avere di completamento automatico match di parole con caratteri speciali, come quelli in tedesco: o, a, e, o ß. Ad esempio, mi piacerebbe "mun" per corrispondenza "München" e "Munchen".
Soluzione
C'è un ottimo articolo su questo a A List Apart che include un po 'di codice Javascript
var accentMap = {
'á': 'a',
'é': 'e',
'í': 'i',
'ó': 'o',
'ú': 'u'
};
function accent_fold(s) {
if (!s) {
return '';
}
var ret = '';
for (var i = 0; i < s.length; i++) {
ret += accent_map[s.charAt(i)] || s.charAt(i);
}
return ret;
};
Altri suggerimenti
Io uso typeahead, e dopo ore di sbattere la testa contro un muro è stato semplice come usare utf8_encode sullo script che restituisce il JSON:
utf8_encode (stripslashes ($ variabile));
jQuery UI ha una demo per questo problema (pieghevole accento) sul loro sito: https: // jQueryUI. com / completamento automatico / # pieghevole
$(function() {
var names = ["Jörn Zaefferer", "Scott González", "John Resig"];
var accentMap = {
"á": "a",
"ö": "o"
};
var normalize = function(term) {
var ret = "";
for (var i = 0; i < term.length; i++) {
ret += accentMap[term.charAt(i)] || term.charAt(i);
}
return ret;
};
$("#developer").autocomplete({
source: function(request, response) {
var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
response($.grep(names, function(value) {
value = value.label || value.value || value;
return matcher.test(value) || matcher.test(normalize(value));
}));
}
});
});
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow