I have a search box on my site and i need to replace all the Spanish characters that user types in to equivalent English alphabets. I have coded which is shown below. This is not working when i plug in it to the my Project and even when create a simple html page which coded same. My page is using <meta charset="utf-8" />
. This is working fine when i created a fiddle http://jsfiddle.net/KJAy3/ . This is how it it is show in the debugger
What am i doing wrong? What kind of encoding i am supposed to us? This Replace method is triggered before the form submit.
function encodeSearch(term){
term=term.replace("á","a");
term=term.replace("Á","A");
term=term.replace("é","e");
term=term.replace("É","E");
term=term.replace("í","i");
term=term.replace("Í","I");
term=term.replace("ó","o");
term=term.replace("Ó","O");
term=term.replace("ú","u");
term=term.replace("Ú","U");
term=term.replace("ñ","n");
term=term.replace("Ñ","N");
return term;
}
您是否使用过<script type="application/javascript" charset="utf-8" src="yourfile.js"></script>"
吗?(当然,假设您的文件另存为utf-8。)
If you are going to use those special characters in HTML, it may be an idea to use this String replace
method for all special characters:
[somestring].replace(/[\u0080-\u024F]/g,
function(a) {
return '&#'+a.charCodeAt(0)+';';
});
It will convert all special characters to numeric html entities and prevent the need to type all those characters in your javascript, eg
'ñíóúü¡¿'.replace(.replace(/[\u0080-\u024F]/g,
function(a) {
return '&#'+a.charCodeAt(0)+';';
});
//=> result
// ñíóúü¡¿
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.