慕桂英546537
2019-10-18 14:47:46
給出以下形式:<form> <input name="foo" value="bar"> <input name="hello" value="hello world"></form>我可以使用$.param( .. )構(gòu)造序列化表格:$.param( $('form input') )=> foo=bar&hello=hello+world如何使用JavaScript反序列化上面的String并返回哈希值?例如,$.magicFunction("foo=bar&hello=hello+world")=> {'foo' : 'bar', 'hello' : 'hello world'}參考:jQuery.param( obj )。
3 回答

慕后森
TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個贊
這種簡短的功能方法怎么樣?
function parseParams(str) {
return str.split('&').reduce(function (params, param) {
var paramSplit = param.split('=').map(function (value) {
return decodeURIComponent(value.replace(/\+/g, ' '));
});
params[paramSplit[0]] = paramSplit[1];
return params;
}, {});
}
例:
parseParams("this=is&just=an&example") // Object {this: "is", just: "an", example: undefined}
添加回答
舉報
0/150
提交
取消