javascript和utf-8 /特殊字符的帮助 [英] javascript and utf-8 / special characters help

查看:82
本文介绍了javascript和utf-8 /特殊字符的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题的简短问题:
所以基本上我的问题是:如何设置javascript以使用UTF-8,以便支持,所以我的JavaScript可以添加正确的值到下拉列表选项?



更长的解释:
使用javascript填充下拉列表中的选项在我的主页上。
下拉列表是一种调用php脚本的形式,用于查询mysql数据库。



问题是,我使用特殊字符äö在我的MySQL数据库。
当我查询这样的mysql数据库时:

  SELECT * FROM database WHERE city = $ city 

它找不到任何东西。



这是因为$ city包含由javascript设置的特殊字符。
但是如果设置没有javascript的$ city变量,例如在我的html文档中使用

  option value =åäö

那么它就会起作用。这意味着javascript是问题所在,它会将特殊字符设置为其他内容,并且这会使查询无法找到任何内容。



javascript填充我的下拉列表list:

  if(document.nav_form_main.nav_city_list.value =='4'){
removeAllOptions(document。 nav_form_main.nav_city_list);
addOption(document.nav_form_main.nav_city_list,Göteborg,Göteborg,);
addOption(document.nav_form_main.nav_city_list,goteborg_closeby,Angränsandeområden,);
addOption(document.nav_form_main.nav_city_list,1,Hela Sverige,);
addOption(document.nav_form_main.nav_city_list,other_area, - Väljområdepånytt - ,);
}
函数removeAllOptions(selectbox)
{
var i;
for(i = selectbox.options.length-1; i> = 0; i--)
{
//selectbox.options.remove(i);
selectbox.remove(i);



$ b函数addOption(selectbox,value,text)
{
var optn = document.createElement(OPTION );
optn.text = text;
optn.value = value;

selectbox.options.add(optn);
}

我的HTML主文档使用:ISO 8859-1



当回复ÅÄÖ时,它在我的PHP代码中起作用。
但是,当从JavaScript中回应变量时,由WEIRD符号替换

解决方案

在收到服务器... ...

 
函数encode_utf8(s){
return unescape(encodeURIComponent(s));
}

函数decode_utf8(s){
return decodeURIComponent(escape(s));
}


SHORT VERSION OF QUESTION: So basically my question is: How can I set the javascript to use UTF-8 so that å ä ö are supported, so my javascript can add the correct values to the drop list options?

LONGER EXPLANATION: Im using javascript to fill 'options' in a drop down list on my main page. The drop down lists are in a form which calls a php script, which querys a mysql database.

The problem is, Im using special characters such as å ä ö in my mysql database. And when I query the mysql database like this:

    SELECT * FROM database WHERE city = $city

it doesnt find anything.

This because $city contains special characters set by the javascript. But if setting the $city variable without javascript, for example in my html document with

      option value=åäö

then it will work. This means the javascript is the problem, and it sets the special characters to something else instead, and this makes the query not to find anything.

javascript that populates my drop down list:

                    if(document.nav_form_main.nav_city_list.value == '4'){
        removeAllOptions(document.nav_form_main.nav_city_list);
        addOption(document.nav_form_main.nav_city_list, "Göteborg",             "Göteborg", "");
        addOption(document.nav_form_main.nav_city_list, "goteborg_closeby", "Angränsande områden", "");
        addOption(document.nav_form_main.nav_city_list, "1", "Hela Sverige", "");
        addOption(document.nav_form_main.nav_city_list, "other_area", "-- Välj område på nytt --", "");
    }
    function removeAllOptions(selectbox)
    {
var i;
for(i=selectbox.options.length-1;i>=0;i--)
{
    //selectbox.options.remove(i);
    selectbox.remove(i);
}
    }


    function addOption(selectbox, value, text )
    {
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;

selectbox.options.add(optn);
    }

MY HTML MAIN DOCUMENT USES: ISO 8859-1

WHEN ECHOING "ÅÄÖ" it works in my PHP code. BUT WHEN ECHOING THE VARIABLE FROM THE JAVASCRIPT THE ÅÄÖ IS REPLACED BY WEIRD SYMBOLS

解决方案

Try these before sending to/after receiving from server...

function encode_utf8( s ) {
  return unescape( encodeURIComponent( s ) );
}

function decode_utf8( s ) {
  return decodeURIComponent( escape( s ) );
}

这篇关于javascript和utf-8 /特殊字符的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆