转换HTML表格JSON
问题描述:
在我MVC 3 Razor
观点,我有一个table with check-boxes
如下转换HTML表格JSON
<table border="1" id="permiison">
<thead>
<tr><th style="width:0px"></th><th>Menu Name</th><th>Add</th> <th>Edit</th><th>Authorize</th><th>View</th></tr>
</thead>
<tbody>
<tr id="per_tr">
<td style="width:0px">10</td><td></td><td><input type="checkbox" checked="True" style="width:80px" id="chkAdd"></td><td><input type="checkbox" checked="True" style="width:80px"></td><td><input type="checkbox" checked="True" style="width:80px"></td> <td><input type="checkbox" style="width:80px" checked="True"></td>
</tr>
<tr id="per_tr"><td style="width:0px">11</td><td><span style="width:150px">Group</span></td><td><input type="checkbox" checked="True" style="width:80px" id="chkAdd"></td><td><input type="checkbox" checked="True" style="width:80px"></td><td><input type="checkbox" checked="True" style="width:80px"></td> <td><input type="checkbox" style="width:80px" checked="True"></td>
</tr>
</tbody>
</table>
我想convert the table into json
用的复选框选中值。 如果有任何机构知道请分享该方法。
答
你可以这样做:
(function($){
var getJsonFromTable = function()
{
var rows = [];
$('#permiison tbody tr').each(function(i, n){
var $row = $(n);
rows.push({
id: $row.find('td:eq(0)').text(),
name: $row.find('td:eq(1)').text(),
add: $row.find('td:eq(2) input[type=checkbox]').prop('checked'),
edit: $row.find('td:eq(3) input[type=checkbox]').prop('checked'),
authorize: $row.find('td:eq(4) input[type=checkbox]').prop('checked'),
view: $row.find('td:eq(5) input[type=checkbox]').prop('checked'),
});
});
return JSON.stringify(rows);
};
$(function(){
console.log(getJsonFromTable());
});
})(jQuery);
答
或可考虑在这里和你自己的情况做同样的(它成功地回答问题):
convert a HTML table data into a JSON object in jQuery
祝你好运,希望这对你有所帮助。