如何在javascript中实现类似PHP的http_build_query和反向的东西? [英] How to implement something like PHP's http_build_query and the reverse in javascript?
本文介绍了如何在javascript中实现类似PHP的http_build_query和反向的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<?php
$data = array('foo'=>'bar',
'baz'=>'boom',
'cow'=>'milk',
'php'=>'hypertext processor');
echo http_build_query($data);
/* Output:
foo=bar&baz=boom&cow=milk&php=hypertext+processor
*/
如何在javascript中执行类似操作,比如从数组获取查询字符串,并转换数组到查询字符串?
How to do similar thing in javascript,say, get the query string from the array ,and convert the array to query string?
更新
jquery插件无效:
the jquery plugin is not working:
var fromVar = $.query.load('?cow=milk')
fromVar.set('first', 'value');
fromVar.toString()
输出?cow = milk
虽然我希望它是?cow = milk& first = value
推荐答案
如果你正在使用jQuery,那么你可以使用 jQuery.param ()功能:
If you're using jQuery, then you can use the jQuery.param() function:
var obj = { "foo":"bar", "baz":"boom", "php":"hypertext processor" };
var str = jQuery.param(obj);
alert(str); // should be "foo=bar&baz=boom&php=hypertext+processor"
它可以序列化一些复杂的数组。
It can serialise some complex arrays too.
这篇关于如何在javascript中实现类似PHP的http_build_query和反向的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文