zoukankan      html  css  js  c++  java
  • 跳转链接,带中文参数乱码的问题

    这个遇到的几率比较小,但还是记录一下

    主要是当跳转链接时,要携带输入的中文参数后,要获取链接里的参数值,直接获取就会出现乱码

    解决方案,在提交时中文部分使用encodeURI ()进行编码,注意这个方法要调用2次,

    在需要获取时,对之前编码部分进行decodeURI()解码 ,这个方法调用一次就好。

    适用于,2个html页面之间的跳转

    <body>
    	<input type="text" id="inp" value="热门"/>
    	<button class="btn1">点击我1</button>
    	<script>
    		$(function() {
    			console.log(location.href);
    			var key1 = GetQueryString("val1");
    			var key2 = decodeURI(GetQueryString("val2"))
    				
    			console.log(key1)  //热门
    			console.log(key2)  //热门
    				
    					
    			$(".btn1").on("click", function() {
    				location.href = location.href+"?f=1&val1="+ $("#inp").val() +"&val2="+encodeURI(encodeURI($("#inp").val()));
    					
    			})
    		})
    		//获取地址栏参数的方法
    		function GetQueryString(name) {
    			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    			var r = window.location.search.substr(1).match(reg);
    			if(r != null) return unescape(r[2]);
    				return null;
    			}
    	</script>
    		
    </body>
    

      

  • 相关阅读:
    位图
    3. 资源管理(条款:13-17)
    70. Implement strStr() 与 KMP算法
    69. Letter Combinations of a Phone Number
    68. Longest Common Prefix
    67. Container With Most Water
    66. Regular Expression Matching
    65. Reverse Integer && Palindrome Number
    波浪理论
    MACD理解
  • 原文地址:https://www.cnblogs.com/qqing/p/8463179.html
Copyright © 2011-2022 走看看