jqueryURL中参数中解决中文乱码问题的两种方法

一、正则分析法 

function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);  //获取url中"?"符后的字符串并正则匹配            
    var context = "";             
    if (r != null)
        context = r[2];             
	reg = null;             
	r = null; 
    return context == null || context == "" || context == "undefined" ? "" : context;         
}     

调用方法:

alert(GetQueryString("参数名1")); 


二、

function GetRequest() { 
var url = location.search; //获取url中"?"符后的字串 
var theRequest = new Object(); 
if (url.indexOf("?") != -1) { 
var str = url.substr(1); 
strs = str.split("&"); 
for(var i = 0; i < strs.length; i ++) { 
theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]); 
} 
} 
return theRequest; 
} 

调用方法:

var Request = new Object(); 
Request = GetRequest(); 
var 参数1,参数2; 
参数1 = Request['参数1']; 

参数2 = Request['参数2'];


如果参数中含有中文字符,注意转编码和解码: 

1、传参页面

location.href = "${ctx}/web/resourceList?u=admin&keyword=" + encodeURI($("#keyword").val());

2、接收参数页面

decodeURI(GetQueryString("keyword"))



你可能感兴趣的:(jquery,+,js,+,ajax)