简体   繁体   English

javascript和utf-8 /特殊字符帮助

[英]javascript and utf-8 / special characters help

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? 问题的简短版本:因此,基本上我的问题是:如何设置javascript以使用UTF-8,以便支持åäö,以便我的javascript可以将正确的值添加到下拉列表选项中?

LONGER EXPLANATION: Im using javascript to fill 'options' in a drop down list on my main page. 更长的解释:我正在使用javascript在主页上的下拉列表中填写“选项”。 The drop down lists are in a form which calls a php script, which querys a mysql database. 下拉列表的形式为调用php脚本的形式,该脚本查询mysql数据库。

The problem is, Im using special characters such as å ä ö in my mysql database. 问题是,我在mysql数据库中使用了åöö等特殊字符。 And when I query the mysql database like this: 当我像这样查询mysql数据库时:

    SELECT * FROM database WHERE city = $city

it doesnt find anything. 它什么也没找到。

This because $city contains special characters set by the javascript. 这是因为$ city包含由javascript设置的特殊字符。 But if setting the $city variable without javascript, for example in my html document with 但是,如果在没有javascript的情况下设置$ city变量,例如在我的html文档中,

      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是问题所在,并且将特殊字符设置为其他字符,这使得查询找不到任何内容。

javascript that populates my drop down list: 填充我的下拉列表的javascript:

                    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 我的HTML主要文档使用:ISO 8859-1

WHEN ECHOING "ÅÄÖ" it works in my PHP code. 当选择“ÅÄÖ”时,它将在我的PHP代码中起作用。 BUT WHEN ECHOING THE VARIABLE FROM THE JAVASCRIPT THE ÅÄÖ IS REPLACED BY WEIRD SYMBOLS 但是在从Javascript中选择变量时,会用怪异的符号替换ÅÄÖ

Here is how I solved this problem: 这是我解决此问题的方法:

document.getElementById("somewhere").innerHTML="mytext";
myoption = document.createElement("option");
myoption.value="myvalue";
myoption.text=document.getElementById("somewhere").innerHTML;
myselect.add(myoption,null);

"somewhere" is an unused, or hidden, or special created element in the page. “某处”是页面中未使用,隐藏或特殊创建的元素。

when javascript reads from innerHTML, you get the correct characters. 当javascript从innerHTML读取时,您将获得正确的字符。

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 ) );
}

To avoid any kind of problems you should make sure you treat your content properly both at the client and server sides. 为避免出现任何类型的问题,您应该确保在客户端和服务器端都正确对待内容。

With PHP: utf8_encode() and utf8_decode() 使用PHP: utf8_encode()utf8_decode()

With Javascript use use webtoolkit.utf8.js : Utf8.encode() and Utf8.decode() 使用Javascript时,请使用webtoolkit.utf8.jsUtf8.encode()Utf8.decode()

As far as I can see, you problem is on the server side, not the client side. 据我所知,您的问题出在服务器端,而不是客户端。 In other words, the problem is in your php, not your javascript. 换句话说,问题出在您的php中,而不是您的javascript中。 Try making a simple php script to output your javascript request. 尝试制作一个简单的PHP脚本以输出您的JavaScript请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM