我有像这样的html inpit字段
<input type="hidden" value="[{"id":3,"menu_name":"testdata1","table_name":"tbl_testdata1","category_name":"networking"},{"id":5,"menu_name":"newbook","table_name":"tbl_newbook1","category_name":"networking"},{"id":4,"menu_name":"books","table_name":"tbl_books","category_name":"system"}]" id="resultdata" name="resultdata">
我尝试使用以下方式获取此文件的值:
$(function() {
var data=$('#resultdata').val();
var categories = {},
groupBy = "category_name",
ul = $('<ul id="dropdown-menu">');
for (var i = 0; i < data.length; i++)
{
if (!categories[data[i][groupBy]])
categories[data[i][groupBy]] = [];
categories[data[i][groupBy]].push(data[i]);
};
for (key in categories)
{
if (categories.hasOwnProperty(key))
{
var li = $('<li>').text(key);
if (categories[key].length)
{
var ul_inner = $('<ul>');
for (var i = 0; i < categories[key].length; i++)
{
$('<li>').html(categories[key][i].menu_name + ', ' + categories[key][i].id).appendTo(ul_inner);
}
li.append(ul_inner);
}
li.appendTo('#tree1');
}
}
$('#tree1').treed();
});
我试图将此结果附加到 ,但它将结果显示为未定义。我需要以json格式本身隐藏的输入字段值。怎么可能?
答案 0 :(得分:0)
你的问题是你的双引号(&#34;)。将value=""
更改为value=''
,它应该可以正常工作。
因为您当前输入的值看起来像value="[{"id"
,那么您的输入中会包含大量无效数据
<强>演示强>
var data = $('#resultdata').val();
console.log(data)
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="hidden" value='[{" id ":3,"menu_name ":"testdata1 ","table_name ":"tbl_testdata1 ","category_name ":"networking "},{"id ":5,"menu_name ":"newbook ","table_name ":"tbl_newbook1 ","category_name ":"networking "},{"id ":4,"menu_name ":"books","table_name ":"tbl_books ","category_name ":"system "}]' id="resultdata" name="resultdata">
&#13;