我有以下代码,它们将用户在html页面中的某些字段上输入的内容转换为阿拉伯字符:

window.onload = function KeyboardChange(){


    var elements = document.querySelectorAll('.arabicChange');
    for (var i = elements.length - 1; i >= 0; i--) {
        elements[i].onkeydown = function myFunction(e){

  var keyCode = window.event ? window.event.keyCode : e.which;
  if(keyCode == 66) {
              this.value += "ل";
              this.value +="ا";
              return false;
  }
  //this case is for deleting one character
  if (keyCode == 8){
              this.value = this.value.substr(0,length(this.value)-1);


      }
  if (keyCode == 9){

      if (this == document.forms[0].ancienNomPropriete)
           document.forms[0].nomPropriete.focus();
      if (this == document.forms[0].nomPropriete)
           document.forms[0].dateBornage.focus();
   }

  else{
      var KEYS_MAP= {
                32 : " ",
                97 : "1",
                98 : "2",
                99 : "3",
                100: "4",
                101: "5",
                102: "6",
                103: "7",
                104: "8",
                105: "9",

                65 :"ض",

                90 :"ص",

                69 :"ث",


                82 :"ق",


                84 :"ف",


                89 :"غ",


                85 :"ع",


                73 :"ه",


                79 :"خ",


                80 :"ح",


                221 :"ج",


                81 :"ش",


                83 :"س",


                68 :"ي",


                70 :"ب",


                71 :"ل",


                72 :"ا",


                74 :"ت",


                75 :"ن",


                76 :"م",


                77 :"ك",


                192 :"ط",


                220 :"ذ",


                87 :"ئ",


                88 :"ء",


                67 :"ؤ",


                86 :"ر",


                66 :"ل",


                78 :"ى",


                188 :"ة",


                190 :"و",


                191 :"ز",


                223 :"ظ",

                186 : "د"

       };
      if(keyCode && KEYS_MAP[keyCode]){
              this.value += KEYS_MAP[keyCode];
              return false;
    }
  };
  return false ;
};
    };



};


我从jsp页面调用此脚本。该脚本不起作用,在Firefox中进行调试后,阿拉伯字母显示在问号中。像这样 :

window.onload = function KeyboardChange(){


        var elements = document.querySelectorAll('.arabicChange');
        for (var i = elements.length - 1; i >= 0; i--) {
            elements[i].onkeydown = function myFunction(e){

      var keyCode = window.event ? window.event.keyCode : e.which;
      if(keyCode == 66) {
                  this.value += "�";
                  this.value +="�";
                  return false;
      }
      //this case is for deleting one character
      if (keyCode == 8){
                  this.value = this.value.substr(0,length(this.value)-1);


          }
      if (keyCode == 9){

          if (this == document.forms[0].ancienNomPropriete)
               document.forms[0].nomPropriete.focus();
          if (this == document.forms[0].nomPropriete)
               document.forms[0].dateBornage.focus();
       }

      else{
          var KEYS_MAP= {
                    32 : " ",
                    97 : "1",
                    98 : "2",
                    99 : "3",
                    100: "4",
                    101: "5",
                    102: "6",
                    103: "7",
                    104: "8",
                    105: "9",

                    65 :"�",

                    90 :"�",

                    69 :"�",


                    82 :"�",


                    84 :"�",


                    89 :"�",


                    85 :"�",


                    73 :"�",


                    79 :"�",


                    80 :"�",


                    221 :"�",


                    81 :"�",


                    83 :"�",


                    68 :"�",


                    70 :"�",


                    71 :"�",


                    72 :"�",


                    74 :"�",


                    75 :"�",


                    76 :"�",


                    77 :"�",


                    192 :"�",


                    220 :"�",


                    87 :"�",


                    88 :"�",


                    67 :"�",


                    86 :"�",


                    66 :"�",


                    78 :"�",


                    188 :"�",


                    190 :"�",


                    191 :"�",


                    223 :"�",

                    186 : "�"

           };
          if(keyCode && KEYS_MAP[keyCode]){
                  this.value += KEYS_MAP[keyCode];
                  return false;
        }
      };
      return false ;
    };
        };



    };


我的jsp页面以以下代码行开头:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html"%>
<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>
<%@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic"%>
<%@ taglib prefix="nested" uri="http://struts.apache.org/tags-nested"%>
<%@ taglib uri="/WEB-INF/struts-layout.tld" prefix="layout" %>
<%@ page import="fr.improve.struts.taglib.layout.workflow.LayoutPlugin" %>

<html>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Language" content="ar-ma">
<link href="pages/style/main.css" rel="stylesheet" type="text/css">
<script language="JavaScript1.2" src="pages/KeyboardAutomaticChange.js"> </script>


为什么阿拉伯字符显示问号?

最佳答案

尝试在HTML中添加charset="UTF-8"

它应该看起来像这样

<script language="JavaScript1.2" src="pages/KeyboardAutomaticChange.js" charset="UTF-8"> </script>


如果失败,则您的JavaScript文件KeyboardAutomaticChange.js可能使用非utf-8编码保存。

我建议在文本编辑器中创建一个新的javascript文件,该文件已配置为以utf-8编码保存文件,然后将KeyboardAutomaticChange.js的内容复制粘贴到此文件中。

10-07 14:12