html code is like this
And the cities.js is like this:
Nothing seems wrong but it doesnt work
Code:
<html> <head> <script language="jscript" src="cities.js" type="text/jscript"></script> <script language="javascript" type="text/javascript"> function onLoad() { try {FillCitiesAndCounties("_cityID", "_countyID", 0, 0);} catch(ex) {} } window.attachEvent("onload", onLoad); </script> </head> <body> <form> <tr> <td><label>City</label></td> <td><select id="_cityID" name="cityid" style="width:100%;"><option value="0">(All)</option></select></td> <td><label>County</label></td> <td><select id="_countyID" name="countyid" style="width:100%;"></select></td> </tr> </form> </body>
And the cities.js is like this:
Code:
var cities = new Array("", "America", "England", "Germany"); var counties = new Array(); counties[0] = new Array(""); counties[1] = new Array("", "New York", "Washington"); counties[2] = new Array("London", "WEstminister"); counties[3] = new Array("Berlin", "Kohln"); function SortByName(arr, start) { var result = new Array(); for(var i = start; i < arr.length; i++) result.push({Id: i, Name: arr[i]}); result = result.sort(function() { return arguments[0].Name.localeCompare(arguments[1].Name); }); return result; } function FillCitiesAndCounties() { var sel1 = document.getElementById(arguments[0]), sel2 = document.getElementById(arguments[1]); var cityId = (arguments[2] != null) ? arguments[2] : 1, countyId = arguments[3] ? arguments[3] : 0; var arr = SortByName(cities, 1); for(var i = 0; i < arr.length; i++) sel1.addOption(arr[i].Id, arr[i].Name, arr[i].Id == cityId); sel2.addOption(0, "", countyId == 0); arr = SortByName(counties[cityId], counties[cityId][0] == "" ? 1 : 0); for(var i = 0; i < arr.length; i++) sel2.addOption((cityId * 100) + arr[i].Id, arr[i].Name, countyId == (cityId * 100) + arr[i].Id); sel1.attachEvent("onchange", function() { while(sel2.options.length > 0) sel2.options.remove(0); sel2.addOption(0, "", true); var cityId = parseInt(event.srcElement.options[event.srcElement.selectedIndex].value); var arr = SortByName(counties[cityId], counties[cityId][0] == "" ? 1 : 0); for(var i = 0; i < arr.length; i++) sel2.addOption((cityId * 100) + arr[i].Id, arr[i].Name, false); }); }
Comment