我如何存储以某种方式保存的 javascript 数组对象,以便我以后可以使用它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7086208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How i can store javascript array object saved somehow so that I can use it later
提问by user882196
I have a situation like follows:
我有如下情况:
I have a id select page where user can select id from the shown ids on the page.When the user select the id i store them as a comma seperated value in a hidden input field.
我有一个 id 选择页面,用户可以从页面上显示的 id 中选择 id。当用户选择 id 时,我将它们存储为隐藏输入字段中的逗号分隔值。
<input type="hidden" values="234,678,987" />
I have a button which on clicking pops up a dialog box which then displays the selected ids. The ids here shown I take from the hidden field. Now I have a requirement to store the product id and name.So probably in this case what should i do because now i can't be able to save this datastructure in hidden input field. Even if i use javascript array ,how can i save that array and use it future for displaying.Is it possible to save array object in hidden input field ....and retrieve it later as array object again...?
我有一个按钮,点击后会弹出一个对话框,然后显示选定的 ID。此处显示的 id 是我从隐藏字段中获取的。现在我需要存储产品 ID 和名称。所以可能在这种情况下我应该怎么做,因为现在我无法在隐藏的输入字段中保存这个数据结构。即使我使用 javascript 数组,我如何保存该数组并在未来使用它进行显示。是否可以将数组对象保存在隐藏的输入字段中......然后再将其作为数组对象检索......?
回答by ghayes
Here's a JSFiddle.
这是一个JSFiddle。
If you want to store the object in an input field, you can use JSON.stringify()
:
如果要将对象存储在输入字段中,可以使用JSON.stringify()
:
//Pass your array (or JSON object) into JSON.stringify:
JSON.stringify([{id:1, product_id: 2, name: 'stack'},
{id: 2, product_id: 2, name: 'overflow'}] )
which will yield:
这将产生:
"[{"id":1,"product_id":2,"name":"stack"},{"id":2,"product_id":2,"name":"overflow"}]"
You can store this value in a hidden field (or a data attribute):
您可以将此值存储在隐藏字段(或数据属性)中:
<input type="hidden" id="project" value="[{'id':1,'product_id':2,'name':'stack"',{'id':2,'product_id':2,'name':'overflow'}]" />
Obviously, you can combine these steps (or do it through PHP / Rails / ...)
显然,你可以结合这些步骤(或者通过 PHP/Rails/...)
$('#project').val(JSON.stringify([1,2,3]));
You can then decode this using, for instance, jQuery:
然后,您可以使用例如 jQuery 对其进行解码:
$.parseJSON($('#project').val());
This should allow you to use the process you are using, but store Javascript objects in your input fields. Hope this helps!
这应该允许您使用您正在使用的流程,但将 Javascript 对象存储在您的输入字段中。希望这可以帮助!
回答by vol7ron
You can
你可以
store the value in memory, using JS
var arr = []; arr.push(your_value);
- keep creating new attribute/values in the dom using JS
- pass the value from page to page through the query string
- store the value in a cookie to be retrieved again
- store the value on the server (in a file/db) to retrieve again
使用 JS 将值存储在内存中
var arr = []; arr.push(your_value);
- 使用 JS 在 dom 中继续创建新的属性/值
- 通过查询字符串将值从页面传递到页面
- 将值存储在 cookie 中以供再次检索
- 将值存储在服务器上(在文件/数据库中)以再次检索
Note:
Options 3 and 5 are only meaningful if you want to go to another page (on the site).
Options 4 and 5 are good if you want to close the browser and retrieve that info at a later point.
注意:
选项 3 和 5 仅在您想转到另一个页面(在站点上)时才有意义。
如果您想关闭浏览器并稍后检索该信息,则选项 4 和 5 是不错的选择。
回答by Joe
If you are not worried about IE7, you can use localStorage:
如果你不担心 IE7,你可以使用localStorage:
window.localStorage.setItem('myArray', JSON.stringify([234,678,987]));
// retrieve
var myArray = JSON.parse(window.localStorage.getItem('myArray'));
The JSON is supported by modern browsers. You can add it by including json2.
现代浏览器支持 JSON。您可以通过包含json2来添加它。
回答by Jacob
I assume you're talking about preserving the data after a new page has loaded. You can use your current approach of storing the data as a string in a hidden input, and then parsethat string into a JavaScript object later. Another option is that your server-side code can produce some inline JavaScript declaring the JavaScript object you need. If you're using ASP.NET, for example, your page could have something like this:
我假设您正在谈论在加载新页面后保留数据。您可以使用当前将数据作为字符串存储在隐藏输入中的方法,然后将该字符串解析为 JavaScript 对象。另一种选择是您的服务器端代码可以生成一些内联 JavaScript 来声明您需要的 JavaScript 对象。例如,如果您使用的是 ASP.NET,您的页面可能是这样的:
// Assuming "serverSideIds" is declared as a List<string>
<% var serializer = new JavaScriptSerializer(); %>
var ids = <%= serializer.Serialize(serverSideIds) %>;