0

さまざまな投稿を見て、関数を使用して URL から値を取得していますが、%20 文字を取り除くことができません。送信されるデータは json です。URL で送信するコードは次のとおりです。

'<a href="' + url +"?id="+ json_data[i].product_id + 
...
"&description="+ json_data[i].description + '"> //includes white spaces

私が持っている他のページで:

function getUrlVars()
{
 var vars = [], hash;
 var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
 for(var i = 0; i < hashes.length; i++)
 {
    hash = hashes[i].split('=');
    vars.push(hash[0]);
    vars[hash[0]] = hash[1];
 }
return vars;
}

var selectedData=getUrlVars();
document.write(selectedData.description); 
4

1 に答える 1

0

encodeURIComponentおよびdecodeURIComponent関数を使用します。

"&description="+ encodeURIComponent(json_data[i].description) + '"

と:

decoreURIComponent(hash[1]);

&これにより、スペースが確保されますが、説明に記号があれば機能します。
例えば:

> hash
["clean", "Machine%20washable%2030%20degrees%20reduced%20spin"]
> decodeURIComponent(hash[1]);
"Machine washable 30 degrees reduced spin"
于 2013-04-16T12:31:18.780 に答える