JavaScript / jQuery中的$ .param()逆函数

给出以下形式:


<form>

    <input name="foo" value="bar">

    <input name="hello" value="hello world">

</form>

我可以使用$.param( .. )构造序列化表格:


$.param( $('form input') )


=> foo=bar&hello=hello+world

如何使用JavaScript反序列化上面的String并返回哈希值?


例如,


$.magicFunction("foo=bar&hello=hello+world")


=> {'foo' : 'bar', 'hello' : 'hello world'}

参考:jQuery.param( obj )。


慕桂英546537
浏览 846回答 3
3回答

肥皂起泡泡

您应该使用jQuery BBQ的deparam函数。经过了充分的测试和记录。

慕后森

这种简短的功能方法怎么样?function parseParams(str) {&nbsp; &nbsp; return str.split('&').reduce(function (params, param) {&nbsp; &nbsp; &nbsp; &nbsp; var paramSplit = param.split('=').map(function (value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return decodeURIComponent(value.replace(/\+/g, ' '));&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; params[paramSplit[0]] = paramSplit[1];&nbsp; &nbsp; &nbsp; &nbsp; return params;&nbsp; &nbsp; }, {});}例:parseParams("this=is&just=an&example") // Object {this: "is", just: "an", example: undefined}
打开App,查看更多内容
随时随地看视频慕课网APP