本文发表在 rolia.net 枫下论坛<code>
<html>
<head>
<title>Test Dynamic Dropdown box</title>
<script language=javascript>
function getSelectedIndex(aSelector) {
for(var i = 0; i<aSelector.options.length;i++)
if (aSelector.options[i].selected)
return i;
return -1;
}
function clearOptions( aSelector ) {
while ( aSelector.length != 0 )
aSelector.options.remove(0);
}
function appendOption( aSelector, aOption ) {
for( var i = 0; i < aSelector.options.length; i++) {
if (aSelector.options[i].text == aOption) return;
}
var aItem = document.createElement("OPTION");
aItem.text = aOption;
aSelector.options.add(aItem);
}
function resetOptions( aSelector, aList ) {
clearOptions(aSelector);
aList.sort();
for( var i = 0; i<aList.length; i++)
appendOption(aSelector,aList[i]);
}
var Select2Options = new Array(
new Array("Option11","Option12"),
new Array("Option21","Option22")
);
function AdjustOptions() {
var n = getSelectedIndex(document.all.select1);
if ( n >= 0 ) {
resetOptions(document.all.select2,Select2Options[n]);
}
}
</script>
</head>
<body>
<form>
<select name="select1" onchange="javascript:AdjustOptions()">
<option selected>Option1</option>
<option>Option2</option>
</select>
<select name="select2">
</select>
<script language=javascript>
document.all.select1.onchange();
// For the initial load
</script>
</form>
</body>
</html>
</code>更多精彩文章及讨论,请光临枫下论坛 rolia.net
<html>
<head>
<title>Test Dynamic Dropdown box</title>
<script language=javascript>
function getSelectedIndex(aSelector) {
for(var i = 0; i<aSelector.options.length;i++)
if (aSelector.options[i].selected)
return i;
return -1;
}
function clearOptions( aSelector ) {
while ( aSelector.length != 0 )
aSelector.options.remove(0);
}
function appendOption( aSelector, aOption ) {
for( var i = 0; i < aSelector.options.length; i++) {
if (aSelector.options[i].text == aOption) return;
}
var aItem = document.createElement("OPTION");
aItem.text = aOption;
aSelector.options.add(aItem);
}
function resetOptions( aSelector, aList ) {
clearOptions(aSelector);
aList.sort();
for( var i = 0; i<aList.length; i++)
appendOption(aSelector,aList[i]);
}
var Select2Options = new Array(
new Array("Option11","Option12"),
new Array("Option21","Option22")
);
function AdjustOptions() {
var n = getSelectedIndex(document.all.select1);
if ( n >= 0 ) {
resetOptions(document.all.select2,Select2Options[n]);
}
}
</script>
</head>
<body>
<form>
<select name="select1" onchange="javascript:AdjustOptions()">
<option selected>Option1</option>
<option>Option2</option>
</select>
<select name="select2">
</select>
<script language=javascript>
document.all.select1.onchange();
// For the initial load
</script>
</form>
</body>
</html>
</code>更多精彩文章及讨论,请光临枫下论坛 rolia.net