如何使用javascript将数据存储为url参数? [英] How to store data as a url parameter using javascript?
本文介绍了如何使用javascript将数据存储为url参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个选项卡式导航菜单,并且为每个选项卡创建了哈希URL.在一个标签中,我有多个信息卡,每个信息卡的右上角都有一个复选框.选中此复选框后,卡的背景颜色会更改.
I have a tabbed navigation menu and I have created hashed URLs for each tab. In one tab I have multiple information cards and a check box in the top right hand corner of each card. when this check box is checked the card's background color changes.
我的目标是,当页面刷新或作为链接发送时,已检查的卡片以及更改的背景颜色将保持选中状态.任何帮助将不胜感激.
My objective is that when the page is refreshed or sent as a link the cards that have been checked remain checked along with the changed background color. Any help would really be appreciated.
推荐答案
您可以使用以下函数:
function dataToQueryString(data){
var encoded = [];
for(var key in data){
// skip prototype properties
if(!data.hasOwnProperty(key)) continue;
// encode string properly to use in url
encoded.push(key + '=' + encodeURIComponent(data[key]));
}
return '?' + encoded.join('&');
}
并像这样使用它:
data = {
id: 1,
name: "John Doe"
}
url = url + dataToQueryString( data );
或更具体的情况:
Or more specific for your case:
function storeCardsInQueryString(cards){
var encoded = [];
for(var i = 0; i < cards.length; i++){
encoded.push('cards[]=' + cards[i]);
}
return '?' + encoded.join('&');
}
并像这样使用它:
cards = [ 1, 7, 21, 22, 53 ];
url = url + storeCardsInQueryString( cards );
这篇关于如何使用javascript将数据存储为url参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文