将隐藏字段值设置为JSON(jQuery / JavaScript)

时间:2010-07-02 15:53:19

标签: javascript jquery json encoding

var c = [{"Id":40,"Action":null,"Card":"0484"}];
$('#hidJson', window.parent.document).val(c);
alert($('#hidJson', window.parent.document).val()); // returns [object Object]
alert($('#hidJson', window.parent.document).val()['Card'); // returns undefined

我在遗留应用中。我不得不提出一个修复,其中IFrame需要将一个JSON数据包返回到parent.document。然后,在我提交父文档后,数据将在服务器端可用。

使用警报来试图弄清楚发生了什么。我是否需要首先编码JSON(在我将它设置在JS的第二行之前。

更新: 我看到它现在正在做什么。它正如此设置DOM中的值。

<input id="hidJson" name="hidJson" type="hidden" value="[object Object]" />

1 个答案:

答案 0 :(得分:0)

是肯定的。否则你将隐藏设置为``c.toString(). firefox has。toSource()`的结果,但这不是可移植的。请参阅http://www.json.org/了解实施情况。

相关问题