如何从Jquery数组创建这种JSON数据
$('.checkedvalue').each(function() {
dataname = $(this).data('value');
key = $(this).data('key');
array_variable = dataname +'_array';
var array_variable = [];
class_label = dataname +'_label';
var class_label = [];
$('.'+dataname).each(function()
{
if($(this).is(':checked'))
{
var value_class = $(this).attr('class');
// console.log($(this).data('classlabel'));
// console.log(value_class);
if(value_class == dataname)
{
array_variable.push($(this).val());
class_label.push($(this).data('classlabel'));
jsonObj[key] = dataname,
jsonObj[class_label] = array_variable;
}
}
});
// console.log(class_label);
});
JSON :
{
"Website Info":"website_contact",
{
"Display" : "index",
"Insert" : "add",
"Login" : "login"
};
}
答案 0 :(得分:0)
<script>
var permissions = {};
$(document).ready(function() {
$("#save_role_permission").click(function()
{
var role = $("#role").val();
var class_name;
$('.checkedvalue').each(function()
{
var controller_val = [];
class_name = $(this).data('value');
controller_label = $(this).data('key');
$('.' + class_name).each(function()
{
if($(this).is(':checked'))
{
var value_class = $(this).attr('class');
method_label = $(this).data('classlabel');
method_name = $(this).val();
permissions[controller_label] = [];
permissions[controller_label].push({[controller_label]:class_name});
if(value_class == class_name)
{
controller_val.push({[method_label]:method_name});
permissions[controller_label].push(controller_val);
}
}
});
});
// console.log(permissions);
// console.log(JSON.stringify(permissions));
// return false;
$.ajax({
url: '<?php echo base_url(); ?>admin/roles/add_role_permission',
type: 'POST',
// dataType: 'json',
// contentType: 'application/json',
data: {
role: role,
permissions:JSON.stringify(permissions),
},
success: function(data)
{
// console.log(data);
location.reload(true);
}
});
});
});
</script>