Jquery 将HTML表转换为JSon
在我的Jquery 将HTML表转换为JSon,jquery,json,html-table,Jquery,Json,Html Table,在我的mvc3razor视图中,我有一个带有复选框的表,如下所示 <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&g
mvc3razor
视图中,我有一个带有复选框的表,如下所示
<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>
菜单名添加编辑视图
10
11组
我想使用复选框的选中值将表转换为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);
您可以执行以下操作:
(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);
或查看此处,并针对您自己的情况执行相同的操作(成功回答了问题):
祝你好运,希望这对你有所帮助。或者看看这里,针对你自己的情况做同样的事情(它成功地回答了问题):
祝你好运,希望这对你有所帮助