我在想你可以尝试这样的事情:
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>
在脚本标签或 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