问题
I have this code :
if (term.length > 0) {
var inputVal = $("#input").val();
if (inputVal.indexOf("jason") !== -1) {
results = $.ui.autocomplete.filter(table, term);
}
}
This works. But when I changed "jason" to "jasön" or "jasün" or something else which is a Turkish character, it doesn't work. Is it because the encoding of the js file? I changed the encoding to Unicode (utf-8 with signature) - Codepage 65001 then Turkish ISO 28599 but it didn't work. Can you tell me what I should do? Thanks.
回答1:
It does recognize Turkish characters, but you are doing an equality check on said string. What you have to understand is that even if ö is only an o with an accent, it's still a different character, thus making your conditional falsy, even if you are not doing a triple equality check.
'ö' == 'o' // false
'ö' === 'o' // false
What you should do instead is convert the input value into a string without accents, since it's apparently what you were expecting. And this question is exactly what you are looking for, and I would say this answer would be the best one if you have access to ES6 features since it's pretty clean and simple to use
function convert (str) {
return str.normalize('NFD').replace(/[\u0300-\u036f]/g, '')
}
if (term.length > 0) {
var inputVal = convert($('#input').val())
if (inputVal.indexOf('jason') !== -1) {
results = $.ui.autocomplete.filter(table, term)
}
}
Otherwise if ES6 is not an option, the top answer should be fine to use, just create a function util that you can reuse anywhere you need to.
Regarding the encoding of the file, you should use utf-8 if dealing with special chars that Turkish does have.
来源:https://stackoverflow.com/questions/44330730/javascript-doesnt-recognize-turkish-characters