2013-01-18 125 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