私はあなたがこのようなことを試すことができると思っていました:
HTML
<table>
<tr>
<td>Category:</td>
<td>
<input type="radio" id="radioTech" name="category" value="1" checked="yes" />Technical
<input type="radio" id="radioNonTech" name="category" value="2" />Non-technical</td>
</tr>
<tr>
<td>Branch</td>
<td>
<select id="options"></select>
</td>
</tr>
</table>
script タグまたは file.js 内
// This is a literal object (JSON), if you want to add more items you just have to add them here.
options = {
'technical': [
'Hardware',
'Networking',
'Linux'
],
'ntechnical': [
'English',
'History',
'Maths'
]
};
var renderItems = function( arrItems, selectElement ){
selectElement.innerHTML = '';
for( var i = 0, l = arrItems.length; i < l; i++ )
{
var item = document.createElement( 'option' );
item.innerHTML = arrItems[i];
selectElement.appendChild( item );
}
};
window.onload = function() {
var radioTechnical = document.getElementById( 'radioTech' );
var radioNonTechnical = document.getElementById( 'radioNonTech' );
var selectElement = document.getElementById( 'options' );
radioTechnical.onclick = function(){ renderItems( options.technical, selectElement ) };
radioNonTechnical.onclick = function(){ renderItems( options.ntechnical, selectElement ) };
radioTechnical.click();
};
この例では、アイテムを動的に追加しています。必要に応じて、外部リソースからデータを取得して JSON オブジェクトに追加することができます。その後、残りの作業が完了し、配列内のアイテムが=Dが表示されます