store在输入隐藏字段中返回json值 [英] store return json value in input hidden field

查看:165
本文介绍了store在输入隐藏字段中返回json值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以将返回json存储在隐藏的输入字段中。例如,这是我的json返回的:

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:

[{"id":"15aea3fa","firstname":"John","lastname":"Doe"}]

我想将id存储在隐藏中因此我可以稍后引用它来做一些事情。

I would like to just store the id in a hidden field so I can reference it later to do something with it.

示例:我有这样的事情:

Example: I have something like this:

<input id="HiddenForId" type="hidden" value="" />

并希望jquery稍后将值返回给我:

and would like jquery to return the value later to me like so:

var scheduletimeid = $('#HiddenForId').val();


推荐答案

您可以将其存储在隐藏字段或存储中它在javascript对象(我的偏好)中,因为可能的访问将通过javascript。

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.

注意:因为你有一个数组,所以这将被访问为<$ c第一个元素的$ c> myvariable [0] (就像你拥有的那样)。

NOTE: since you have an array, this would then be accessed as myvariable[0] for the first element (as you have it).

编辑显示示例:

clip...
            success: function(msg)
            {
                LoadProviders(msg);
            },
...

var myvariable ="";

function LoadProviders(jdata)
{
  myvariable = jdata;
};
alert(myvariable[0].id);// shows "15aea3fa" in the alert



<编辑:编辑:创建此页面: http://jsfiddle.net/GNyQn/ 以演示上述内容。这个例子假设您已经在数组中正确返回了您的命名字符串值,并且只需要根据OP问题存储它。在示例中,我还将第一个数组的值(按照OP示例)放入div作为文本。

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.

我不知道为什么这被视为复杂,因为我看不到在这个数组中处理这些字符串的简单方法。

I am not sure why this has been viewed as "complex" as I see no simpler way to handle these strings in this array.

这篇关于store在输入隐藏字段中返回json值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆