Javascript 在输入隐藏字段中存储返回 json 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3448831/
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
store return json value in input hidden field
提问by hersh
I was wondering if it's possible to store the return json in a hidden input field. For example this is what my json return:
我想知道是否可以将返回的 json 存储在隐藏的输入字段中。例如,这是我的 json 返回的内容:
[{"id":"15aea3fa","firstname":"John","lastname":"Doe"}]
I would like to just store the id in a hidden field so I can reference it later to do something with it.
我只想将 id 存储在一个隐藏字段中,以便我以后可以引用它来做一些事情。
Example: I have something like this:
示例:我有这样的事情:
<input id="HiddenForId" type="hidden" value="" />
and would like jquery to return the value later to me like so:
并希望 jquery 稍后将值返回给我,如下所示:
var scheduletimeid = $('#HiddenForId').val();
采纳答案by Mark Schultheiss
You can store it in a hidden field, OR store it in a javascript object (my preference) as the likely access will be via javascript.
您可以将其存储在隐藏字段中,或者将其存储在 javascript 对象中(我的偏好),因为可能通过 javascript 进行访问。
NOTE: since you have an array, this would then be accessed as myvariable[0]for the first element (as you have it).
注意:由于您有一个数组,因此可以像myvariable[0]第一个元素一样访问它(因为您拥有它)。
EDIT show example:
编辑显示示例:
clip...
success: function(msg)
{
LoadProviders(msg);
},
...
var myvariable ="";
function LoadProviders(jdata)
{
myvariable = jdata;
};
alert(myvariable[0].id);// shows "15aea3fa" in the alert
EDIT: Created this page:http://jsfiddle.net/GNyQn/to demonstrate the above. This example makes the assumption that you have already properly returned your named string values in the array and simply need to store it per OP question. In the example, I also put the values of the first array returned (per OP example) into a div as text.
编辑:创建此页面:http: //jsfiddle.net/GNyQn/以演示上述内容。此示例假设您已经在数组中正确返回了命名字符串值,并且只需要根据 OP 问题存储它。在示例中,我还将返回的第一个数组的值(每个 OP 示例)作为文本放入 div 中。
I am not sure why this has been viewed as "complex" as I see no simpler way to handle these strings in this array.
我不确定为什么这被视为“复杂”,因为我认为没有更简单的方法来处理这个数组中的这些字符串。
回答by rodu
Although I have seen the suggested methods used and working, I think that setting the value of an hidden field only using the JSON.stringify breaks the HTML...
虽然我已经看到使用和工作的建议方法,但我认为仅使用 JSON.stringify 设置隐藏字段的值会破坏 HTML...
Here I'll explain what I mean:
在这里,我将解释我的意思:
<input type="hidden" value="{"name":"John"}">
As you can see the first double quote after the open chain bracket could be interpreted by some browsers as:
正如您所看到的,开链括号后的第一个双引号可以被某些浏览器解释为:
<input type="hidden" value="{" rubbish >
So for a better approach to this I would suggest to use the encodeURIComponentfunction. Together with the JSON.stringify we shold have something like the following:
因此,为了更好地解决这个问题,我建议使用encodeURIComponent函数。连同 JSON.stringify 我们应该有如下内容:
> encodeURIComponent(JSON.stringify({"name":"John"}))
> "%7B%22name%22%3A%22John%22%7D"
Now that value can be safely stored in an input hidden type like so:
现在该值可以安全地存储在输入隐藏类型中,如下所示:
<input type="hidden" value="%7B%22name%22%3A%22John%22%7D">
or (even better) using the data- attribute of the HTML element manipulated by the script that will consume the data, like so:
或者(甚至更好)使用由将使用数据的脚本操作的 HTML 元素的 data- 属性,如下所示:
<div id="something" data-json="%7B%22name%22%3A%22John%22%7D"></div>
Now to read the data back we can do something like:
现在要读回数据,我们可以执行以下操作:
> var data = JSON.parse(decodeURIComponent(div.getAttribute("data-json")))
> console.log(data)
> Object {name: "John"}
回答by Mic
You can use input.value = JSON.stringify(obj)to transform the object to a string.
And when you need it back you can use obj = JSON.parse(input.value)
您可以使用input.value = JSON.stringify(obj)将对象转换为字符串。
当你需要它回来时,你可以使用obj = JSON.parse(input.value)
The JSON object is available on modern browsers or you can use the json2.js library from json.org
JSON 对象在现代浏览器上可用,或者您可以使用来自json.org的 json2.js 库
回答by dxh
If you use the JSON Serializer, you can simply store your object in string format as such
如果您使用JSON Serializer,您可以简单地以字符串格式存储您的对象
myHiddenText.value = JSON.stringify( myObject );
You can then get the value back with
然后你可以用
myObject = JSON.parse( myHiddenText.value );
However, if you're not going to pass this value across page submits, it might be easier for you, and you'll save yourself a lot of serialization, if you just tuck it away as a global javascript variable.
但是,如果您不打算在页面提交之间传递这个值,对您来说可能会更容易,并且如果您将它作为全局 javascript 变量隐藏起来,您将节省大量序列化时间。
回答by jmar777
It looks like the return value is in an array? That's somewhat strange... and also be aware that certain browsers will allow that to be parsed from a cross-domain request (which isn't true when you have a top-level JSON object).
看起来返回值在一个数组中?这有点奇怪......还要注意某些浏览器会允许从跨域请求中解析它(当您拥有顶级 JSON 对象时,这不是真的)。
Anyway, if that is an array wrapper, you'll want something like this:
无论如何,如果这是一个数组包装器,你会想要这样的东西:
$('#my-hidden-field').val(theObject[0].id);
You can later retrieve it through a simple .val() call on the same field. This honestly looks kind of strange though. The hidden field won't persist across page requests, so why don't you just keep it in your own (pseudo-namespaced) value bucket? E.g.,
您可以稍后通过对同一字段的简单 .val() 调用来检索它。老实说,这看起来有点奇怪。隐藏字段不会跨页面请求持续存在,那么为什么不将它保存在您自己的(伪命名空间)值桶中呢?例如,
$MyNamespace = $MyNamespace || {};
$MyNamespace.myKey = theObject;
This will make it available to you from anywhere, without any hacky input field management. It's also a lot more efficient than doing DOM modification for simple value storage.
这将使您可以从任何地方使用它,而无需任何繁琐的输入字段管理。对于简单的值存储,它也比做 DOM 修改要高效得多。
回答by JohnSmith
just set the hidden field with javascript :
只需使用 javascript 设置隐藏字段:
document.getElementById('elementId').value = 'whatever';
or do I miss something?
或者我错过了什么?

