2012-01-28 50 views
4

我使用serialize()來獲取表單值,爲了獲取值我分割序列化的字符串,但值是uri編碼,像'@'被替換爲'%40',我用decodeURIComponent()來解碼,問題看起來像解決了,但我仍然把空格換成'+'符號。可以使用string.replace(),但它會替換字符串中我的合法'+'符號。如何實現它?反序列化表值

回答

3

如果字符串中有合法的+,則它將被編碼爲%2B。因此,在對字符串進行調用decodeURIComponent()之前,用空格替換表示字符串中空格的所有+,然後調用decodeURIComponent()來解碼字符串。

使用此代碼

var str = "%4Bseri%2Balized+String+plus" 
str = str.replace(/\+/g, " "); 
str = decodeURIComponent(str); 
alert(str); 

Demo