gpt4 book ai didi

jquery - 如何通过循环在ajax jquery中创建json数组并将其传递给rails Controller

转载 作者:行者123 更新时间:2023-12-01 01:50:05 28 4
gpt4 key购买 nike

我有以下字符串

{
"role_name": "bhavin",
"user_id": "50667afb9de0f2600b000001",
"Par_user": "user",
"Child_user_index": "user_index",
"Child_user_profile": "user_profile",
"Par_App": "App",
"Child_App_new": "App_new",
"Child_App_index": "App_index",
"Par_App1": "App1",
"Child_App1_index": "App1_index",
"Par_App2": "App2",
"Child_App2_index": "App2_index",
"Child_App2_new": "App2_new",
"controller": "users",
"action": "save_permissions"
}​

我想通过以下

{
"role_name": "bhavin",
"user_id": "50667afb9de0f2600b000001",
"user": ["user_index", "user_profile"],
"App": ["App_new", "App_index"],
"App1": ["App1_index"],
"App2": ["App2_index", "App2_new"],
"controller": "users",
"action": "save_permissions"
}​

代码:

$("#perm_save_button").click(function() { //alert("hiii");
if ($("#role_name").val() == "") {
alert("role name can't be blank");
return false;
}

if ($('input:checkbox').is(":checked")) {

} else {
alert("Please Select any Privilages");
return false;
}
var role_name = $('#role_name').val();
var user_id = $("#app_rateid").val();
if (($("#AllPrev").is(":checked"))) {

var selected_value = new Object();

$('input[checked=checked]').each(function() {
$('span[class=checked]')
var select = ($(this).attr('id'));
var select_value = ($(this).attr('name'));
selected_value[select] = select_value;
});
selected_value["role_name"] = role_name;
selected_value["user_id"] = app_rateid;

$.ajax({
url: '<%= url_for :action => "save_permissions" %>',
data: selected_value,
dataType: 'json',
success: function(data) {
alert("101 Data Successfully Updated");
$("#rma_options").dialog('close');
}
}); //ajax function end
}
else {
var selected_value = new Object();
selected_value["role_name"] = role_name;
selected_value["user_id"] = user_id;
$('input[checked=checked]').each(function() {
var select = ($(this).attr('id'));
var select_value = ($(this).attr('name'));
select_value = select_value.split(/_(.+)?/)[1]
alert(select_value);
selected_value[select] = select_value;
});

$.ajax({
url: '<%= url_for :action => "save_permissions" %>',
data: selected_value,
dataType: 'json',
success: function(data) {

alert("101 Data Successfully Updated");
$("#rma_options").dialog('close');
}
}); //ajax function end
}

});​

最佳答案

我在想一些类似的事情

var start = {
"role_name": "bhavin",
"user_id": "50667afb9de0f2600b000001",
"Par_user": "user",
"Child_user_index": "user_index",
"Child_user_profile": "user_profile",
"Par_App": "App",
"Child_App_new": "App_new",
"Child_App_index": "App_index",
"Par_App1": "App1",
"Child_App1_index": "App1_index",
"Par_App2": "App2",
"Child_App2_index": "App2_index",
"Child_App2_new": "App2_new",
"controller": "users",
"action": "save_permissions"
};

var end = {};
$.each(start, function(key, value) {
if (/Par_App/.test(key)) {
if (!end['App']) end['App'] = [];
end['App'].push(key);
}
else if (/Child_user/.test(key)) {
if (!end['user']) end['user'] = [];
end['user'].push(key);
}
});

console.log(JSON.stringify(end));

顺便说一句,它还没有完成。 :)工作fiddle

关于jquery - 如何通过循环在ajax jquery中创建json数组并将其传递给rails Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12948191/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com