我需要在保存使用JSON对象创建的角色和权限矩阵时获得JSON响应。
这是我创建角色和权限矩阵(表格)的代码:
<table id="perm">
<tr class="header">
<th>Permissions</th>
</tr>
</table>
JS:
$(document).ready(function () {
var $table = $( "#perm" );
var $tHead = $table.find( ".header" );
var permissions = ['',"R", "W", "D", "WS" ]
// Count and add Roles
var roles = [];
var numRoles = 0;
for( i in obj ) {
if( roles[ obj[ i ].ProjectRoleID ] == undefined ) {
roles[ obj[ i ].ProjectRoleID ] = obj[ i ].ProjectRoleID;
numRoles++;
$tHead.append( '<th colspan="4" class="role_' + obj[ i ].ProjectRoleID + '">' + obj[ i ].RoleName + '</th>' );
}
}
// Load table
for( i in obj ) {
var element = obj[ i ];
// Add screen row
if( $table.find( "tr.row_" + element.ScreenID ).length == 0 ) {
$table.append( '<tr class="row_' + element.ScreenID + '"><td>' + element.ScreenName + '</td></tr>' );
$row = $table.find( '.row_' + element.ScreenID );
for( j in roles ) {
var role = roles[ j ];
for( k = 1; k < 5; k++ ) {
var uniqueCheck = "check_" + element.ScreenID + "_" + role + "_" + k;
$row.append( '<td class="cell_' + role + '"><input id="' + uniqueCheck + '" name="' + uniqueCheck + '" type="checkbox"> ' + permissions[ k ] + ' </td>' );
}
}
}
var checkId = "#check_" + element.ScreenID + "_" + element.ProjectRoleID + "_" + element.PermissiontypeID;
$( checkId ).attr( "checked", "checked" );
}
});
这是我的矩阵(表):
我需要获取已选中复选框的值。请帮忙。提前谢谢。
答案 0 :(得分:2)
轻微修改代码
var $table = $("#perm");
var $tHead = $table.find(".header");
var permissions = ['', "R", "W", "D", "WS"]
// Count and add Roles
var roles = [];
var numRoles = 0;
for (i in obj) {
if (roles[obj[i].ProjectRoleID] == undefined && obj[i].ProjectRoleID != null) {
roles[obj[i].ProjectRoleID] = obj[i].ProjectRoleID;
numRoles++;
$tHead.append('<th colspan="4" value = "' + obj[i].ProjectRoleID + '" class="role_' + obj[i].ProjectRoleID + '">' + obj[i].RoleName + '</th>');
}
}
// Load table
for (i in obj) {
var element = obj[i];
// Add screen row
if (element.ScreenID != null) {
if ($table.find("tr.row_" + element.ScreenID).length == 0) {
$table.append('<tr class="Permission row_' + element.ScreenID + '" value = "' + element.ScreenID + '"><td>' + element.ScreenName + '</td></tr>');
$row = $table.find('.row_' + element.ScreenID);
for (j in roles) {
var role = roles[j];
for (k = 1; k < 5; k++) {
var uniqueCheck = "check_" + element.ScreenID + "_" + role + "_" + permissions[k];
$row.append('<td class="cell_' + role + '"><input id="' + uniqueCheck + '" name="' + permissions[k] + '" type="checkbox" value = "' + role + '"> ' + permissions[k] + ' </td>');
}
}
}
var checkId = "#check_" + element.ScreenID + "_" + element.ProjectRoleID + "_" + element.PermissionType;
$(checkId).attr("checked", "checked");
}
}
//在这里形成json
$(document).on('click', '#YourID', function (e) {
var PermissionDetails = [];
$('tr.Permission td:not(:first-child) input:checked').each(function () {
var ScreenID = $(this).closest('tr').attr('value');
var ProjectRoleID = $(this).attr('value');
var PermissionType = $(this).attr('name');
PermissionDetails.push({ "ScreenID": ScreenID, "ProjectRoleID": ProjectRoleID, "PermissionType": PermissionType });
});
});