我传递的变量为 true,url 和空格由浏览器自动替换为“+”或“%20”。重定向页面后我想正确拆分这个词。
例如:- http://localhost:8008/Login.aspx?stid=Are你快乐
加载登录页面后自动转换,
http://localhost:8008/Login.aspx?stid=Are+You+Happy
或
http://localhost:8008/Login.aspx?stid=Are%20You%20Happy
如何在加载页面后获取此 stid 变量,
你快乐吗
有没有办法使用 javascript 或 jquery 来做到这一点??
我希望在发送参数时得到相同的结果,
http://localhost:8008/Login.aspx?stid=Are+You+Happy
我也想要下面应该喜欢的结果。
是+你+快乐
这两个功能都可以通过 javascript 或 jquery 完成吗?
最佳答案
//decode function
function decode(encodedStr) {
return decodeURIComponent(encodedStr.replace(/\+/g, " "));
}
//encode function
function encode(unencoded ) {
return encodeURIComponent(unencoded).replace(/'/g,"%27").replace(/"/g,"%22");
}
你也可以使用JQuery.params() :
var myObject = {
stid: 'You are happy',
b: [ 1, 2, 3 ],
.......
};
var recursiveEncoded = $.param( myObject );
var recursiveDecoded = decodeURIComponent( $.param( myObject ) );
alert( recursiveEncoded );
alert( recursiveDecoded );
关于javascript - 特殊字符替换为空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35407691/