跳轉鏈接,帶中文參數亂碼的問題


這個遇到的幾率比較小,但還是記錄一下

主要是當跳轉鏈接時,要攜帶輸入的中文參數后,要獲取鏈接里的參數值,直接獲取就會出現亂碼

解決方案,在提交時中文部分使用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>

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM