2012-08-14 100 views
0

在我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用的复选框选中值。 如果有任何机构知道请分享该方法。

回答

3

你可以这样做:

(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); 

http://jsfiddle.net/PKFaE/