jquery动态依赖从多维数组中选择

时间:2015-04-01 04:29:38

标签: javascript jquery html multidimensional-array

我正在尝试使用从阵列填充的两个下拉选择选项的表单。第一个选择只有一个数组,但是根据选择,我想用特定的多维数组填充第二个数组。我可以单独执行它们,但是我无法将第一个选择中的数组名称传递给函数以填充第二个。

我是javascript和jQuery 1.11.0的新手,非常感谢您的帮助。

以下是我正在使用的代码。

select name="aircraftList" id="aircraftList" onchange="showAircraft(this.selectedIndex)"

select name="configWBList" id="configWBList"

// values for AIRCRAFT
// tnumber, acmodel, maxwt, f1arm, m1arm, r1arm, littarm, bag1max, fuel1kgmax, 
/////////////////////////////////////////////////////////// 
var aircraft = [
    ["Select Aircraft", "", "", "", "", "", "", "", "", ""],
    ['N901AH', "Agusta A109E", 3000, 1585, 2455, 3200, 5300, 3200, 150, 480],
    ['N902AH', "Agusta A109E", 3000, 1585, 2455, 3200, 5300, 3200, 150, 480],
    ['N903AH', "Agusta A109E", 3000, 1585, 2455, 3200, 5300, 3200, 150, 480],
    ['N904AH', "Agusta A109E", 3000, 1585, 2455, 3200, 5300, 3200, 150, 480],
    ['N905AH', "Agusta A109E", 3000, 1585, 2455, 3200, 5300, 3200, 150, 480],
    ['N907AH', "Agusta A109E", 3000, 1585, 2455, 3200, 5300, 3200, 150, 480]
];

// values for AC N901AH
/////////////////////////////////////////////////////////// 
var N901AH = [
    ["Select a Configuration", "Select Config", "Select Config"],
    ["6 PAX STANDARD SEATS OFFSHORE", 2205.61, 3485.69],
    ["DUAL CONTROLS INSTALLED", 2207.81, 3483.76],
    ["VIP SEATS INSTALLED", 2212.11, 3483.75],
    ["VIP SEATS, DUAL CONTROLS INSTALLED", 2214.31, 3481.83],
    ["ONE LITTER KIT INSTALLED", 2213.91, 3483.50],
    ["ONE LITTER, DUAL CONTROLS INSTALLED", 2216.11, 3481.58],
    ["6 PAX STANDARD SEATS, VESTS REMOVED", 2196.81, 3489.83],
    ["VIP SEATS INSTALLED, VEST REMOVED", 2203.31, 3487.87],
    ["ONE LITTER INSTALLED, VESTS REMOVED", 2207.31, 3487.02],
    ["TWO LITTER INSTALLED", 2222.21, 3481.32],
    ["TWO LITTER, DUAL CONTROLS INSTALLED", 2224.41, 3479.41],
    ["VIP SEATS, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2205.51, 3485.93],
    ["ONE LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2209.51, 3485.09],
    ["TWO LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2220.01, 3482.33],
    ["FLOATS, RAFT, VESTS REMOVED", 2084.51, 3507.26]
];

// values for AC N902AH
/////////////////////////////////////////////////////////// 
var N902AH = [
    ["Select a Configuration", "Select Config", "Select Config"],
    ["6 PAX STANDARD SEATS OFFSHORE", 2205.61, 3485.69],
    ["DUAL CONTROLS INSTALLED", 2207.81, 3483.76],
    ["VIP SEATS INSTALLED", 2212.11, 3483.75],
    ["VIP SEATS, DUAL CONTROLS INSTALLED", 2214.31, 3481.83],
    ["ONE LITTER KIT INSTALLED", 2213.91, 3483.50],
    ["ONE LITTER, DUAL CONTROLS INSTALLED", 2216.11, 3481.58],
    ["6 PAX STANDARD SEATS, VESTS REMOVED", 2196.81, 3489.83],
    ["VIP SEATS INSTALLED, VEST REMOVED", 2203.31, 3487.87],
    ["ONE LITTER INSTALLED, VESTS REMOVED", 2207.31, 3487.02],
    ["TWO LITTER INSTALLED", 2222.21, 3481.32],
    ["TWO LITTER, DUAL CONTROLS INSTALLED", 2224.41, 3479.41],
    ["VIP SEATS, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2205.51, 3485.93],
    ["ONE LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2209.51, 3485.09],
    ["TWO LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2220.01, 3482.33],
    ["FLOATS, RAFT, VESTS REMOVED", 2084.51, 3507.26]
];

// values for AC N903AH
/////////////////////////////////////////////////////////// 
var N903AH = [
    ["Select a Configuration", "Select Config", "Select Config"],
    ["6 PAX STANDARD SEATS OFFSHORE", 2205.61, 3485.69],
    ["DUAL CONTROLS INSTALLED", 2207.81, 3483.76],
    ["VIP SEATS INSTALLED", 2212.11, 3483.75],
    ["VIP SEATS, DUAL CONTROLS INSTALLED", 2214.31, 3481.83],
    ["ONE LITTER KIT INSTALLED", 2213.91, 3483.50],
    ["ONE LITTER, DUAL CONTROLS INSTALLED", 2216.11, 3481.58],
    ["6 PAX STANDARD SEATS, VESTS REMOVED", 2196.81, 3489.83],
    ["VIP SEATS INSTALLED, VEST REMOVED", 2203.31, 3487.87],
    ["ONE LITTER INSTALLED, VESTS REMOVED", 2207.31, 3487.02],
    ["TWO LITTER INSTALLED", 2222.21, 3481.32],
    ["TWO LITTER, DUAL CONTROLS INSTALLED", 2224.41, 3479.41],
    ["VIP SEATS, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2205.51, 3485.93],
    ["ONE LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2209.51, 3485.09],
    ["TWO LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2220.01, 3482.33],
    ["FLOATS, RAFT, VESTS REMOVED", 2084.51, 3507.26]
];

// values for AC N904AH
/////////////////////////////////////////////////////////// 
var N904AH = [
    ["Select a Configuration", "Select Config", "Select Config"],
    ["6 PAX STANDARD SEATS OFFSHORE", 2205.61, 3485.69],
    ["DUAL CONTROLS INSTALLED", 2207.81, 3483.76],
    ["VIP SEATS INSTALLED", 2212.11, 3483.75],
    ["VIP SEATS, DUAL CONTROLS INSTALLED", 2214.31, 3481.83],
    ["ONE LITTER KIT INSTALLED", 2213.91, 3483.50],
    ["ONE LITTER, DUAL CONTROLS INSTALLED", 2216.11, 3481.58],
    ["6 PAX STANDARD SEATS, VESTS REMOVED", 2196.81, 3489.83],
    ["VIP SEATS INSTALLED, VEST REMOVED", 2203.31, 3487.87],
    ["ONE LITTER INSTALLED, VESTS REMOVED", 2207.31, 3487.02],
    ["TWO LITTER INSTALLED", 2222.21, 3481.32],
    ["TWO LITTER, DUAL CONTROLS INSTALLED", 2224.41, 3479.41],
    ["VIP SEATS, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2205.51, 3485.93],
    ["ONE LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2209.51, 3485.09],
    ["TWO LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2220.01, 3482.33],
    ["FLOATS, RAFT, VESTS REMOVED", 2084.51, 3507.26]
];

// values for AC N905AH
/////////////////////////////////////////////////////////// 
var N905AH = [
    ["Select a Configuration", "Select Config", "Select Config"],
    ["6 PAX STANDARD SEATS OFFSHORE", 2205.61, 3485.69],
    ["DUAL CONTROLS INSTALLED", 2207.81, 3483.76],
    ["VIP SEATS INSTALLED", 2212.11, 3483.75],
    ["VIP SEATS, DUAL CONTROLS INSTALLED", 2214.31, 3481.83],
    ["ONE LITTER KIT INSTALLED", 2213.91, 3483.50],
    ["ONE LITTER, DUAL CONTROLS INSTALLED", 2216.11, 3481.58],
    ["6 PAX STANDARD SEATS, VESTS REMOVED", 2196.81, 3489.83],
    ["VIP SEATS INSTALLED, VEST REMOVED", 2203.31, 3487.87],
    ["ONE LITTER INSTALLED, VESTS REMOVED", 2207.31, 3487.02],
    ["TWO LITTER INSTALLED", 2222.21, 3481.32],
    ["TWO LITTER, DUAL CONTROLS INSTALLED", 2224.41, 3479.41],
    ["VIP SEATS, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2205.51, 3485.93],
    ["ONE LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2209.51, 3485.09],
    ["TWO LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2220.01, 3482.33],
    ["FLOATS, RAFT, VESTS REMOVED", 2084.51, 3507.26]
];

// values for AC N907AH
/////////////////////////////////////////////////////////// 
var N907AH = [
    ["Select a Configuration", "Select Config", "Select Config"],
    ["6 PAX STANDARD SEATS OFFSHORE", 2205.61, 3485.69],
    ["DUAL CONTROLS INSTALLED", 2207.81, 3483.76],
    ["VIP SEATS INSTALLED", 2212.11, 3483.75],
    ["VIP SEATS, DUAL CONTROLS INSTALLED", 2214.31, 3481.83],
    ["ONE LITTER KIT INSTALLED", 2213.91, 3483.50],
    ["ONE LITTER, DUAL CONTROLS INSTALLED", 2216.11, 3481.58],
    ["6 PAX STANDARD SEATS, VESTS REMOVED", 2196.81, 3489.83],
    ["VIP SEATS INSTALLED, VEST REMOVED", 2203.31, 3487.87],
    ["ONE LITTER INSTALLED, VESTS REMOVED", 2207.31, 3487.02],
    ["TWO LITTER INSTALLED", 2222.21, 3481.32],
    ["TWO LITTER, DUAL CONTROLS INSTALLED", 2224.41, 3479.41],
    ["VIP SEATS, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2205.51, 3485.93],
    ["ONE LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2209.51, 3485.09],
    ["TWO LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2220.01, 3482.33],
    ["FLOATS, RAFT, VESTS REMOVED", 2084.51, 3507.26]
];


// set form values to selected aircraft option  
///////////////////////////////////////////////////////////
function showAircraft(idx) {
    var aircraftWB = [aircraft[idx][0]]
    alert(aircraftWB);
    $("#configWBList").html('');
    list(aircraftWB);
}

function list(arrary) {
    alert(arrary);

    // populate selected aircraft w&b select
    ///////////////////////////////////////////////////////////
    for (i = 0; i < arrary.length; i++) {
        var select = document.getElementById("configWBList");
        select.options[select.options.length] = new Option(arrary[i][0], arrary[i][1]);
    }

    // reset the selected aircraft configuration to standard
    ///////////////////////////////////////////////////////////
    $("#configWBList").val($("#configWBList option:first").val());
    $("#configWBList").selectmenu('refresh', true);

}


// initialise form
// ****************************************************
function initForm() {
    // populate aircraft select
    ///////////////////////////////////////////////////////////
    for (i = 0; i < aircraft.length; i++) {
        var select = document.getElementById("aircraftList");
        select.options[select.options.length] = new Option(aircraft[i][0], aircraft[i][1]);
    }
}

initForm();

https://jsfiddle.net/g3h9p2ur/

2 个答案:

答案 0 :(得分:2)

我建议从阵列中创建一个对象,如:

var aircrafts = { "N901AH" : N901AH, "N902AH" : N902AH, "N903AH" : N903AH, "N904AH" : N904AH, "N905AH" : N905AH, "N907AH" : N907AH};

而不是for循环使用$ .each(array,handler(index,element))。然后使用选项文本而不是eval()来从对象获取所需的数组:

console.log(aircrafts["N901AH"]);//example

并填充第二个选择。 这是我的小提琴: jsfiddle for my answer

问候。

答案 1 :(得分:0)

你可以使用&#34; eval&#34;获取结果变量数据。

&#13;
&#13;
<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script language="javascript">
var aircraft = [
    ["Select Aircraft", "", "", "", "", "", "", "", "", ""],
    ['N901AH', "Agusta A109E", 3000, 1585, 2455, 3200, 5300, 3200, 150, 480],
    ['N902AH', "Agusta A109E", 3000, 1585, 2455, 3200, 5300, 3200, 150, 480],
    ['N903AH', "Agusta A109E", 3000, 1585, 2455, 3200, 5300, 3200, 150, 480],
    ['N904AH', "Agusta A109E", 3000, 1585, 2455, 3200, 5300, 3200, 150, 480],
    ['N905AH', "Agusta A109E", 3000, 1585, 2455, 3200, 5300, 3200, 150, 480],
    ['N907AH', "Agusta A109E", 3000, 1585, 2455, 3200, 5300, 3200, 150, 480]
];

// values for AC N901AH
/////////////////////////////////////////////////////////// 
var N901AH = [
    ["Select a Configuration1", "Select Config", "Select Config"],
    ["6 PAX STANDARD SEATS OFFSHORE", 2205.61, 3485.69],
    ["DUAL CONTROLS INSTALLED", 2207.81, 3483.76],
    ["VIP SEATS INSTALLED", 2212.11, 3483.75],
    ["VIP SEATS, DUAL CONTROLS INSTALLED", 2214.31, 3481.83],
    ["ONE LITTER KIT INSTALLED", 2213.91, 3483.50],
    ["ONE LITTER, DUAL CONTROLS INSTALLED", 2216.11, 3481.58],
    ["6 PAX STANDARD SEATS, VESTS REMOVED", 2196.81, 3489.83],
    ["VIP SEATS INSTALLED, VEST REMOVED", 2203.31, 3487.87],
    ["ONE LITTER INSTALLED, VESTS REMOVED", 2207.31, 3487.02],
    ["TWO LITTER INSTALLED", 2222.21, 3481.32],
    ["TWO LITTER, DUAL CONTROLS INSTALLED", 2224.41, 3479.41],
    ["VIP SEATS, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2205.51, 3485.93],
    ["ONE LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2209.51, 3485.09],
    ["TWO LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2220.01, 3482.33],
    ["FLOATS, RAFT, VESTS REMOVED", 2084.51, 3507.26]
];

// values for AC N902AH
/////////////////////////////////////////////////////////// 
var N902AH = [
    ["Select a Configuration2", "Select Config", "Select Config"],
    ["6 PAX STANDARD SEATS OFFSHORE", 2205.61, 3485.69],
    ["DUAL CONTROLS INSTALLED", 2207.81, 3483.76],
    ["VIP SEATS INSTALLED", 2212.11, 3483.75],
    ["VIP SEATS, DUAL CONTROLS INSTALLED", 2214.31, 3481.83],
    ["ONE LITTER KIT INSTALLED", 2213.91, 3483.50],
    ["ONE LITTER, DUAL CONTROLS INSTALLED", 2216.11, 3481.58],
    ["6 PAX STANDARD SEATS, VESTS REMOVED", 2196.81, 3489.83],
    ["VIP SEATS INSTALLED, VEST REMOVED", 2203.31, 3487.87],
    ["ONE LITTER INSTALLED, VESTS REMOVED", 2207.31, 3487.02],
    ["TWO LITTER INSTALLED", 2222.21, 3481.32],
    ["TWO LITTER, DUAL CONTROLS INSTALLED", 2224.41, 3479.41],
    ["VIP SEATS, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2205.51, 3485.93],
    ["ONE LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2209.51, 3485.09],
    ["TWO LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2220.01, 3482.33],
    ["FLOATS, RAFT, VESTS REMOVED", 2084.51, 3507.26]
];

// values for AC N903AH
/////////////////////////////////////////////////////////// 
var N903AH = [
    ["Select a Configuration3", "Select Config", "Select Config"],
    ["6 PAX STANDARD SEATS OFFSHORE", 2205.61, 3485.69],
    ["DUAL CONTROLS INSTALLED", 2207.81, 3483.76],
    ["VIP SEATS INSTALLED", 2212.11, 3483.75],
    ["VIP SEATS, DUAL CONTROLS INSTALLED", 2214.31, 3481.83],
    ["ONE LITTER KIT INSTALLED", 2213.91, 3483.50],
    ["ONE LITTER, DUAL CONTROLS INSTALLED", 2216.11, 3481.58],
    ["6 PAX STANDARD SEATS, VESTS REMOVED", 2196.81, 3489.83],
    ["VIP SEATS INSTALLED, VEST REMOVED", 2203.31, 3487.87],
    ["ONE LITTER INSTALLED, VESTS REMOVED", 2207.31, 3487.02],
    ["TWO LITTER INSTALLED", 2222.21, 3481.32],
    ["TWO LITTER, DUAL CONTROLS INSTALLED", 2224.41, 3479.41],
    ["VIP SEATS, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2205.51, 3485.93],
    ["ONE LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2209.51, 3485.09],
    ["TWO LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2220.01, 3482.33],
    ["FLOATS, RAFT, VESTS REMOVED", 2084.51, 3507.26]
];

// values for AC N904AH
/////////////////////////////////////////////////////////// 
var N904AH = [
    ["Select a Configuration", "Select Config", "Select Config"],
    ["6 PAX STANDARD SEATS OFFSHORE", 2205.61, 3485.69],
    ["DUAL CONTROLS INSTALLED", 2207.81, 3483.76],
    ["VIP SEATS INSTALLED", 2212.11, 3483.75],
    ["VIP SEATS, DUAL CONTROLS INSTALLED", 2214.31, 3481.83],
    ["ONE LITTER KIT INSTALLED", 2213.91, 3483.50],
    ["ONE LITTER, DUAL CONTROLS INSTALLED", 2216.11, 3481.58],
    ["6 PAX STANDARD SEATS, VESTS REMOVED", 2196.81, 3489.83],
    ["VIP SEATS INSTALLED, VEST REMOVED", 2203.31, 3487.87],
    ["ONE LITTER INSTALLED, VESTS REMOVED", 2207.31, 3487.02],
    ["TWO LITTER INSTALLED", 2222.21, 3481.32],
    ["TWO LITTER, DUAL CONTROLS INSTALLED", 2224.41, 3479.41],
    ["VIP SEATS, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2205.51, 3485.93],
    ["ONE LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2209.51, 3485.09],
    ["TWO LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2220.01, 3482.33],
    ["FLOATS, RAFT, VESTS REMOVED", 2084.51, 3507.26]
];

// values for AC N905AH
/////////////////////////////////////////////////////////// 
var N905AH = [
    ["Select a Configuration", "Select Config", "Select Config"],
    ["6 PAX STANDARD SEATS OFFSHORE", 2205.61, 3485.69],
    ["DUAL CONTROLS INSTALLED", 2207.81, 3483.76],
    ["VIP SEATS INSTALLED", 2212.11, 3483.75],
    ["VIP SEATS, DUAL CONTROLS INSTALLED", 2214.31, 3481.83],
    ["ONE LITTER KIT INSTALLED", 2213.91, 3483.50],
    ["ONE LITTER, DUAL CONTROLS INSTALLED", 2216.11, 3481.58],
    ["6 PAX STANDARD SEATS, VESTS REMOVED", 2196.81, 3489.83],
    ["VIP SEATS INSTALLED, VEST REMOVED", 2203.31, 3487.87],
    ["ONE LITTER INSTALLED, VESTS REMOVED", 2207.31, 3487.02],
    ["TWO LITTER INSTALLED", 2222.21, 3481.32],
    ["TWO LITTER, DUAL CONTROLS INSTALLED", 2224.41, 3479.41],
    ["VIP SEATS, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2205.51, 3485.93],
    ["ONE LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2209.51, 3485.09],
    ["TWO LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2220.01, 3482.33],
    ["FLOATS, RAFT, VESTS REMOVED", 2084.51, 3507.26]
];

// values for AC N907AH
/////////////////////////////////////////////////////////// 
var N907AH = [
    ["Select a Configuration", "Select Config", "Select Config"],
    ["6 PAX STANDARD SEATS OFFSHORE", 2205.61, 3485.69],
    ["DUAL CONTROLS INSTALLED", 2207.81, 3483.76],
    ["VIP SEATS INSTALLED", 2212.11, 3483.75],
    ["VIP SEATS, DUAL CONTROLS INSTALLED", 2214.31, 3481.83],
    ["ONE LITTER KIT INSTALLED", 2213.91, 3483.50],
    ["ONE LITTER, DUAL CONTROLS INSTALLED", 2216.11, 3481.58],
    ["6 PAX STANDARD SEATS, VESTS REMOVED", 2196.81, 3489.83],
    ["VIP SEATS INSTALLED, VEST REMOVED", 2203.31, 3487.87],
    ["ONE LITTER INSTALLED, VESTS REMOVED", 2207.31, 3487.02],
    ["TWO LITTER INSTALLED", 2222.21, 3481.32],
    ["TWO LITTER, DUAL CONTROLS INSTALLED", 2224.41, 3479.41],
    ["VIP SEATS, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2205.51, 3485.93],
    ["ONE LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2209.51, 3485.09],
    ["TWO LITTER, DUAL CONTROLS INSTALLED, VESTS REMOVED", 2220.01, 3482.33],
    ["FLOATS, RAFT, VESTS REMOVED", 2084.51, 3507.26]
];
//alert(eval('N901AH'));
var subListValue="";
$( document ).ready(function() {
//alert(aircraft.length);

   for(i=0;i<aircraft.length;i++)
   {
	
	 $('#list1')
         .append($("<option></option>")
         .attr("value",aircraft[i][0])
         .text(aircraft[i][0]));
		 
		
   }
   
   
   $('#list1').on('change', function() {
   $('#list2').empty();
  subListValue=eval(this.value ); // or $(this).val()
  for(i=0;i<subListValue.length;i++)
   {
	
	 $('#list2')
         .append($("<option></option>")
         .attr("value",subListValue[i][0])
         .text(subListValue[i][0]));
		 
		
   }
});
});
</script>
<select id="list1">
</select>
<select id="list2">
</select>
</html>
&#13;
&#13;
&#13;