如何将变量存储到存储到另一个数组(jQuery)的数组中

时间:2013-04-04 19:11:03

标签: jquery arrays variables

对于我正在研究的项目,有几个按钮可以打开不同的模态弹出窗口。每个窗口都有自己的复选框。

用户选择一些复选框然后关闭模式后,复选框信息将存储在一个数组中并显示在页面上。

我的问题是,当用户重新打开相同的模态弹出窗口时,我需要填写相同的复选框。我找到了stackoverflow question which covers this here.

但是我的问题是,似乎我必须创建一个唯一的变量/数组,然后将该数组放入主数组中,我不知道该怎么做。

// id of the button so I can use the name to open unique popups
// and create unique variable names
var roleId = '';

// The main Array to store all my button/checkbox lists Arrays into
var storedArray = [];    

// The Array that stores the currently selected checkbox items.
var selectedArray = [];


// The function that is on all the buttons that bring up the checkbox modals
$('.role').click(function(){

    // Saves a unique ID to be used to open up the appropriate popup
    roleId = $(this).attr('tag');

    // Here I try to create a Variable for a unique Array
    storedArray.push('stored'+roleName);

    // Selects the appropriate Modal popup
    $('#modal-'+roleId).modal();
    return false;
});

// Later the function that saves the checkbox items that are selected:
    $('input:checked').each(function() {
        selectedArray.push($(this).val());
    });

// Where I try to store the checkbox data into the
// storedArray which is inside the slected Array
    storedArray.push(selectedArray);
    console.log('2 storedArray = '+storedArray);

1 个答案:

答案 0 :(得分:1)

查看SimpleModal的文档,有一个persist选项。这样做你想要的吗?

$('#modal-'+roleId).modal({persist:true});