[Beschreibung]
Dieses Script schränkt die Auswahlmöglichkeiten beim Einsatz mehrerer Dropdownlisten ein.
[Kompatibilität]
[Code]
//Author: Paul Berry - PBerry@CirrusSoftware.com //Date: July 2002 //Licence: Its Freeware!... Just leave my name against the code and send me a mail if you like. //Purpose: Provides simple dropdown functionality that could be used on multiple cascading dropdown lists. //Compatability: This process will work on both IE and Netscape (tested on Ns 4.5 and IE 5.5)
var iArrayMax = 16 var aDropdown = new Array(iArrayMax)
//as the page loads - first thing to do is to load the dropdown array var bOk = LoadArrays()
function LoadArrays() { //this can be thought of as an object array, each element can be identified as a property of the array position // a very powerful structure for 'client side' data caching. Array[0] = new sElement('UK','En','England') Array[1] = new sElement('UK','Ir','Ireland') Array[2] = new sElement('UK','Sc','Scotland') Array[3] = new sElement('UK','Wa','Wales') Array[4] = new sElement('UK','Ia','Isle of Arran')
Array[5] = new sElement('USA','Ny','New York') Array[6] = new sElement('USA','Il','Illinois') Array[7] = new sElement('USA','Fl','Florida') Array[8] = new sElement('USA','Nm','New Mexico') Array[9] = new sElement('USA','Tx','Texas') Array[10] = new sElement('USA','Se','Seattle') Array[11] = new sElement('USA','NJ','New Jersey') Array[12] = new sElement('USA','VT','Vermont') Array[13] = new sElement('USA','AZ','Arizona') Array[14] = new sElement('USA','NH','New Hampshire') Array[15] = new sElement('USA','MI','Missouri') Array[16] = new sElement('USA','NE','New England') Array[17] = new sElement('USA','PA','Pennsylavania') Array[18] = new sElement('USA','Se','Seattle')
return true }
function sElement(sParentId,sValue,sDescription) { // elements that will be loaded into the array structure and persisted // think of it as an object. this.ParentId = sParentId this.Id = sValue this.Description = sDescription }
function bCascadeDrop(oDDsource,oDDdest) { //function to enable cascading dropdowns //called as the parent dropdown changes. var iX var sText var iY= 0 var sOptionId var sOptionDesc var iStartPos
//find the value of the item currently selected sText = oDDsource.options[oDDsource.selectedIndex].value if (sText != '0') { //clear down the destination list box oDDdest.options.length = 0
//loop through the elements that are in the array // if they match the parent if then they should be displayed. for (iX=0; iX<=iArrayMax; iX++) { if(sText == Array[iX].ParentId) { //grab the values out of the element (Netscape is not happy doing too many things in a function call!) sOptionId = Array[iX].Id sOptionDesc= Array[iX].Description //alert(sOptionDesc) //write the element into the dripdown box. oDDdest.options[iY] = new Option (sOptionDesc,sOptionId) iY = iY +1 } } } } </script>
//write the element into the dripdown box.
<form action="FrmNew"> <table> <tr> <td>Country</td> <td> <select style="width=150" name="sCountry" onChange="bCascadeDrop(this, document.forms[0].sRegion)"> <option value="0" SELECTED> - None selected - </option> <option value="UK" > United Kingdom</option> <option value="USA">America</option> </select> </td> </tr> <tr> <td>Region</td> <td> <select name="sRegion" style="width=150"> <option value="0" SELECTED> - None selected - </option> </select> </td> </tr>
</table> </form>
Paul Berry
Kopieren Sie bitte den Code