码迷,mamicode.com
首页 > Web开发 > 详细

HTML的Get方法URL传递中文参数,解决乱码问题

时间:2016-08-26 10:29:23      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:

本例中有使用JQuery。

资料参考:http://www.cnblogs.com/babycool/p/3169058.html

 

发送的HTML页面代码:

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="UTF-8">
 5 <title>Index</title>
 6 <script type="text/javascript" charset="uf-8" src="jquery-1.11.3.min.js"></script>
 7 <script>
 8     $(function() {
 9         $(#send).click(function() {
10             var value1 = $(#value1).val();
11             var value2 = $(#value2).val();
12             var url = "receive.html?value1=" + value1 + "&value2=" + value2;
13             url=encodeURI(url);   //对URL的地址进行encodeURI编码,实际上只有中文的部分被修改编码
14             window.location.href=url;
15         });
16     });
17 </script>
18 </head>
19 <body>
20     <div>
21         <div>
22             <span>value1:&nbsp;</span><input type="text" id="value1" />
23         </div>
24         <div>
25             <span>value2:&nbsp;</span><input type="text" id="value2" />
26         </div>
27         <div>
28             <button id="send">发送</button>
29         </div>
30     </div>
31 </body>
32 </html>    

 

接收的HTML页面代码:

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="UTF-8">
 5 <title>Receive Param</title>
 6 <script type="text/javascript" charset="utf-8" src="cordova-2.1.0.js"></script>
 7 <script type="text/javascript" charset="uf-8" src="jquery-1.11.3.min.js"></script>
 8 <script>
 9     function getParam(name) {
10         var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
11         var r = window.location.search.substr(1).match(reg);
12         if (r != null)
13             return decodeURI(r[2]);   //对参数进行decodeURI解码
14         return null;
15     }
16 
17     $(function() {
18         var val1 = getParam("value1");
19         var val2 = getParam("value2");
20         $(#value1).html(val1);
21         $(#value2).html(val2);
22     });
23 </script>
24 </head>
25 <body>
26     <div>
27         <div>
28             <span>value1: </span><b id="value1"></b>
29         </div>
30         <div>
31             <span>value2: </span><b id="value2"></b>
32         </div>
33     </div>
34     <hr />
35     <div>
36         <a href="index.html">back</a>
37     </div>
38 </body>
39 </html>

 

HTML的Get方法URL传递中文参数,解决乱码问题

标签:

原文地址:http://www.cnblogs.com/chenyucong/p/5809201.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!