简体   繁体   English

在级联下拉列表所依赖的数组中间添加一个值

[英]Adding a value in the middle of an array on which cascading dropdown relies

I may eventually have "Title 1E". 我可能最终会拥有“ Title 1E”。 This poses a problem as the corresponding Array below would, either, require me to create ["Title 1E", '8'] (which I wouldn't want to do) or create placeholders for all potential Title 1's (ie change to ["Title 2A", '100'] and create blank Title 1's 4-99), thereby making a very long menu filled with blank lines. 这带来了一个问题,因为下面的相应数组要么要求我创建["Title 1E", '8'] (我不想这样做),要么为所有可能的Title 1创建占位符(即更改为["Title 2A", '100']并创建空白标题1的4-99),从而制作一个非常长的菜单,并用空白行填充。

I'd manually insert the appropriate Array in the subsubmenu 我会在子subsubmenu手动插入适当的数组

If submenu[0].push(4,1," "Title 1E", '4' ") works, how can I change the following to ["Title 2A", '5'] 如果submenu[0].push(4,1," "Title 1E", '4' ")有效,如何将以下内容更改为["Title 2A", '5']

Hope it makes some sense. 希望这有道理。 It's not all quite clear in my head. 我脑子里还不太清楚。

(function() {
var submenu= [
        [   ["Title 1A", '0'],
            ["Title 1B", '1'],
            ["Title 1C", '2'],
            ["Title 1D", '3']
        ],

        [       ["Title 2A", '4'],
            ["Title 2B", '5'],
            ["Title 2C", '6'], 
            ["Title 2D", '7']  
        ],


];

//the Array below populates a sub-submenu when a selection is made above


var subsubmenu= [           

[   ["Issue 1A1", 'resonse1a1'],
        ["Issue 1A2", 'response1a2'],
        ["Issue 1A3", 'response1a3'],
        ["Issue 1A4", 'response1a4']
    ],

    [   ["Issue 1B1", 'resonse1b1'],
        ["Issue 1B2", 'resonse1b2'], 
        ["Issue 1B3", 'resonse1b3']
    ],


    [   ["Issue 1C1", 'resonse1c1']
    ],
        // etc...               
        ];

You need to manually loop through the remaining items and increase the counter manually. 您需要手动循环浏览其余项目并手动增加计数器。 Like this 像这样

var submenu= [
        [   ["Title 1A", '0'],
            ["Title 1B", '1'],
            ["Title 1C", '2'],
            ["Title 1D", '3']
        ],

        [       ["Title 2A", '4'],
            ["Title 2B", '5'],
            ["Title 2C", '6'], 
            ["Title 2D", '7']  
        ],
];

var insert_at=0;
submenu[insert_at].push(["Title 1E", '4']);

// Since the item will be pushed at the last position in the corresponding array
// We need to increase the counter i.e. item at position 1 for all the following 
//items in the other arrays
for(i=insert_at+1;i<submenu.length;i++){
    for(j=0;j<submenu[i].length;j++){
        submenu[i][j][1]++;
    }
}

// Now display that to confirm that it works ok
for(i=insert_at+1;i<submenu.length;i++){
    for(j=0;j<submenu[i].length;j++){
        document.write(submenu[i][j]);
    }
}

Check it out at http://jsfiddle.net/gunjankarun/zCjY5/ http://jsfiddle.net/gunjankarun/zCjY5/中查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM