2013-01-18 129 views
1

我是jQuery的新手!通过数据属性将HTML表格元素转换为JSON

有没有办法将表元素数据转换为JSON?看到下面的示例图像。

enter image description here

的JSON结果是在#result股利。

+0

入住这里:使用jQuery通过HTML表格迭代,在表中转换数据到JSON] [1] 但如果你自己控制这个表的生成,我推荐类似淘汰赛(http://knockoutjs.com/) [1]:http://stackoverflow.com/questions/1872485/iterate-through-html-table-using-jquery-converting-the-data-在表中插入j –

回答

1
var arr = $('tr').map(function() { 
    var o = { 
     "apvhdrid": this.parentNode.parentNode.dataset.apvhdrid, 
     "id": this.dataset.id 
    }; 
    $('td', this).each(function() { 
     for (key in this.dataset) { 
      o[key] = this.dataset[key]; 
     } 
    }) 
    return o; 
}).get(); 

$('#result').text(JSON.stringify(arr)); 

http://jsfiddle.net/pE8Hu/

+0

魔法!谢谢你! 这就是我需要做的! http://goo.gl/jX5hL – jrsalunga