日期:2014-05-16  浏览次数:20523 次

在HTML页面<script>标签 里弹出的文字乱码
在HTML页面<script>标签 里弹出的文字乱码
也面是gb2312 JS 是UTF-8的 HTML里的中文都是转了UTF-8  
  怎么解决乱码 加了charset 也诶用

<script type="text/javascript"> 
function beforeSubmit(form1){
if(form1.username.value==''){
alert('用户不能为空 !');
form1.username.focus();
return false;
}
if(form1.password.value==''){
alert('密码不能为空!');
form1.password.focus();
return false;
}
if(form1.password.value.length<6){
alert('密码至少为6位,请重新输入!');
form1.password.focus();
return false;
}

return true;
}
</script>

------解决方案--------------------
记事本打开 另存为 编码选 与 页面meta定义的编码 一致。
------解决方案--------------------
这个不会乱码

HTML code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
<script type="text/javascript">  
function beforeSubmit(form1){
if(form1.username.value==''){
alert('用户不能为空 !');
form1.username.focus();
return false;
}
if(form1.password.value==''){
alert('密码不能为空!');
form1.password.focus();
return false;
}
if(form1.password.value.length<6){
alert('密码至少为6位,请重新输入!');
form1.password.focus();
return false;
}

return true;
}
</script>
</head>
<form id="form1" onsubmit="beforeSubmit(this);">
    用户:
    <input type="text" id="username"/>
    <br />
    密码:
    <input type="password" id="password"/>
    <input type="submit" value="提交" />
</form>
<body>
</body>
</html>

------解决方案--------------------
刚写错了,不是unicode编码,是16进制转换,比如alert("\u9ad8\u8d35\u6a58\u9ec4")