選択オプションを動的に作成したい。私は2つの配列「年」と「月」を持っています。ユーザーが 2011 を選択すると、それぞれの月が次のドロップダウンに表示されます。例えば
2009 を選択した場合、次のドロップダウン オプションは「12 月」、「10 月」、「2 月」です。
<head>
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function (){
$('#first').change(function(){
var year= new Array('2011','2010','2009');
var month= new Array('jan_feb_march','jan_march_april','dec_oct_feb');
var yearVal= $('#first option:selected').text();
for(i=0; i<year.length;i++){
if(year[i]==yearVal){
var months=month[i]
}
}
var y= months.split('_');
for (z=0;z<y.length;z++){
$('#second').append("<option>'"+y[z]+"'</option>")
}
})
})
</script>
</head>
<body>
<div class="menu">
<select id="first">
<option>select</option>
<option>2011</option>
<option>2010</option>
<option>2009</option>
</select>
<select id="second"></select>
</div>
</body>