简体   繁体   中英

How to pass an array parameter with javascript

I've to pass an ids array from a page to another page. I create $ids in PHP and I use use this with jQuery in this way:

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.

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(',') + ']';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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