简体   繁体   English

如何使用javascript传递数组参数

[英]How to pass an array parameter with javascript

I've to pass an ids array from a page to another page.我必须将一个ids数组从一个页面传递到另一个页面。 I create $ids in PHP and I use use this with jQuery in this way:我在 PHP 中创建$ids并以这种方式在 jQuery 中使用它:

var ids = <?php echo json_encode($ids); ?>;

jQuery(ids).each(function() {
    filters.push('ids[]=' + jQuery(this));
});

The URL result is the following:网址结果如下:

http://url.it/?sort=newest&ids[]=[object%20Object]&ids[]=[object%20Object]&ids[]=[object%20Object]&ids[]=[object%20Object]&ids[]=[object%20Object]

I would like to have in the URL an array with all elements but I obtain all arrays with one element.我想在 URL 中包含一个包含所有元素的数组,但我获得了一个包含一个元素的所有数组。

Can you help me?你能帮助我吗?

var params = ids.map(id => 'ids[]=' + encodeURIComponent(id))
filters.concat(params)

you do not need to do extra things.你不需要做额外的事情。 Just create your link like this:只需像这样创建您的链接:

var ids = <?= json_encode($ids); ?>;
var link = 'http://url.it/?sort=newest&ids=[' + ids.join(',') + ']';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM