webForumDet fria alternativet

Fel i funktion

6 svar · 319 visningar · startad av Insider

InsiderMedlem sedan jan. 2000978 inlägg
#1

Denna koden ska ta värdet från en drop-meny och populera den andra menyn med värdet som är valt i den första.

Felmeddelande i eplorer: "Ett objekt förväntas"

Vad är fel???

function PopulateAreaCode() {
   // Only process the function if the first item is not selected.
   if (document.StateForm.StateCode.selectedIndex != 0) {
      // Find the state abbreviation
      var ThisState = document.StateForm.StateCode[document.StateForm.StateCode.selectedIndex].value;
      // Set the length of the arecode drop down equal to the length of the state's array
      document.StateForm.AreaCode.length = eval("StateArray" + ThisState + ".length");
      // Put 'Select' as the first option in the area code drop-down
      document.StateForm.AreaCode[0].value = "";
      document.StateForm.AreaCode[0].text = "Select";
      document.StateForm.AreaCode[0].selected = true;
      // Loop through the state's array and populate the area code drop down.
      for (i=1; i<eval("StateArray" + ThisState + ".length"); i++) {
         document.StateForm.AreaCode[i].value = eval("StateArray" + ThisState + "[i]");
         document.StateForm.AreaCode[i].text = eval("StateArray" + ThisState + "[i]");
      }
   }
}
....
....
....
....
<select name="StateCode" onChange="PopulateAreaCode()">
@ndersMedlem sedan juni 200032 969 inlägg
#2

En liten grej som jag ser direkt:

var ThisState = document.StateForm.StateCode[b].options[/b][document.StateForm.StateCode.selectedIndex].value;

Mvh

InsiderMedlem sedan jan. 2000978 inlägg
#3

Hmmm, jag får samma fel.....den reagerade inte på ändringen.

InsiderMedlem sedan jan. 2000978 inlägg
#4

Här kommer hela sidan:

När man ändrar län i den vänstra listan ska dess kommuner komma upp i den högra

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
	<title>Lista service per område</title>
</head>
<body>

<!-- Select the states and area codes. -->

<script language = "JavaScript">	
<!--
// For each state, create an array to hold the area codes.
// Each state array will be identified by the two-character state abbreviation

   // Create the array
   StateArray10 = new Array();
   
   // Populate the array
   
      StateArray10[1] = Karlshamn;
   
      StateArray10[2] = Karlskrona;
   
      StateArray10[3] = Olofström;
   
      StateArray10[4] = Ronneby;
   
      StateArray10[5] = Sölvesborg;
   

   // Create the array
   StateArray09 = new Array();
   
   // Populate the array
   
      StateArray09[1] = Gotland;
   

   // Create the array
   StateArray13 = new Array();
   
   // Populate the array
   
      StateArray13[1] = Falkenberg;
   
      StateArray13[2] = Halmstad;
   
      StateArray13[3] = Hylte;
   
      StateArray13[4] = Kungsbacka;
   
      StateArray13[5] = Laholm;
   
      StateArray13[6] = Varberg;
   

   // Create the array
   StateArray06 = new Array();
   
   // Populate the array
   
      StateArray06[1] = Aneby;
   
      StateArray06[2] = Eksjö;
   
      StateArray06[3] = Gislaved;
   
      StateArray06[4] = Gnosjö;
   
      StateArray06[5] = Habo;
   
      StateArray06[6] = Jönköping;
   
      StateArray06[7] = Mullsjö;
   
      StateArray06[8] = Nässjö;
   
      StateArray06[9] = Sävsjö;
   
      StateArray06[10] = Tranås;
   
      StateArray06[11] = Vaggeryd;
   
      StateArray06[12] = Vetlanda;
   
      StateArray06[13] = Värnamo;
   

   // Create the array
   StateArray08 = new Array();
   
   // Populate the array
   
      StateArray08[1] = Borgholm;
   
      StateArray08[2] = Emmaboda;
   
      StateArray08[3] = Hultsfred;
   
      StateArray08[4] = Högsby;
   
      StateArray08[5] = Kalmar;
   
      StateArray08[6] = Mönsterås;
   
      StateArray08[7] = Mörbylånga;
   
      StateArray08[8] = Nybro;
   
      StateArray08[9] = Oskarshamn;
   
      StateArray08[10] = Torsås;
   
      StateArray08[11] = Vimmerby;
   
      StateArray08[12] = Västervik;
   

   // Create the array
   StateArray07 = new Array();
   
   // Populate the array
   
      StateArray07[1] = Alvesta;
   
      StateArray07[2] = Lessebo;
   
      StateArray07[3] = Ljungby;
   
      StateArray07[4] = Markaryd;
   
      StateArray07[5] = Tingsryd;
   
      StateArray07[6] = Uppvidinge;
   
      StateArray07[7] = Växjö;
   
      StateArray07[8] = Älmhult;
   

   // Create the array
   StateArray12 = new Array();
   
   // Populate the array
   
      StateArray12[1] = Bjuv;
   
      StateArray12[2] = Bromölla;
   
      StateArray12[3] = Burlöv;
   
      StateArray12[4] = Båstad;
   
      StateArray12[5] = Eslöv;
   
      StateArray12[6] = Helsingborg;
   
      StateArray12[7] = Hässleholm;
   
      StateArray12[8] = Höganäs;
   
      StateArray12[9] = Hörby;
   
      StateArray12[10] = Höör;
   
      StateArray12[11] = Klippan;
   
      StateArray12[12] = Kristianstad;
   
      StateArray12[13] = Kävlinge;
   
      StateArray12[14] = Landskrona;
   
      StateArray12[15] = Lomma;
   
      StateArray12[16] = Lund;
   
      StateArray12[17] = Malmö;
   
      StateArray12[18] = Osby;
   
      StateArray12[19] = Perstorp;
   
      StateArray12[20] = Simrishamn;
   
      StateArray12[21] = Sjöbo;
   
      StateArray12[22] = Skurup;
   
      StateArray12[23] = Staffanstorp;
   
      StateArray12[24] = Svalöv;
   
      StateArray12[25] = Svedala;
   
      StateArray12[26] = Tomelilla;
   
      StateArray12[27] = Trelleborg;
   
      StateArray12[28] = Vellinge;
   
      StateArray12[29] = Ystad;
   
      StateArray12[30] = Åstorp;
   
      StateArray12[31] = Ängelholm;
   
      StateArray12[32] = Örkelljunga;
   
      StateArray12[33] = Östra Göinge;
   

   // Create the array
   StateArray14 = new Array();
   
   // Populate the array
   
      StateArray14[1] = Ale;
   
      StateArray14[2] = Alingsås;
   
      StateArray14[3] = Bengtsfors;
   
      StateArray14[4] = Bollebygd;
   
      StateArray14[5] = Borås;
   
      StateArray14[6] = Dals-Ed;
   
      StateArray14[7] = Essunga;
   
      StateArray14[8] = Falköping;
   
      StateArray14[9] = Färgelanda;
   
      StateArray14[10] = Grästorp;
   
      StateArray14[11] = Gullspång;
   
      StateArray14[12] = Göteborg;
   
      StateArray14[13] = Götene;
   
      StateArray14[14] = Herrljunga;
   
      StateArray14[15] = Hjo;
   
      StateArray14[16] = Härryda;
   
      StateArray14[17] = Karlsborg;
   
      StateArray14[18] = Kungälv;
   
      StateArray14[19] = Lerum;
   
      StateArray14[20] = Lidköping;
   
      StateArray14[21] = Lilla Edet;
   
      StateArray14[22] = Lysekil;
   
      StateArray14[23] = Mariestad;
   
      StateArray14[24] = Mark;
   
      StateArray14[25] = Mellerud;
   
      StateArray14[26] = Munkedal;
   
      StateArray14[27] = Mölndal;
   
      StateArray14[28] = Orust;
   
      StateArray14[29] = Partille;
   
      StateArray14[30] = Skara;
   
      StateArray14[31] = Skövde;
   
      StateArray14[32] = Sotenäs;
   
      StateArray14[33] = Stenungsund;
   
      StateArray14[34] = Strömstad;
   
      StateArray14[35] = Svenljunga;
   
      StateArray14[36] = Tanum;
   
      StateArray14[37] = Tibro;
   
      StateArray14[38] = Tidaholm;
   
      StateArray14[39] = Tjörn;
   
      StateArray14[40] = Tranemo;
   
      StateArray14[41] = Trollhättan;
   
      StateArray14[42] = Töreboda;
   
      StateArray14[43] = Uddevalla;
   
      StateArray14[44] = Ulricehamn;
   
      StateArray14[45] = Vara;
   
      StateArray14[46] = Vårgårda;
   
      StateArray14[47] = Vänersborg;
   
      StateArray14[48] = Åmål;
   
      StateArray14[49] = Öckerö;
   

   // Create the array
   StateArray05 = new Array();
   
   // Populate the array
   
      StateArray05[1] = Boxholm;
   
      StateArray05[2] = Finspång;
   
      StateArray05[3] = Kinda;
   
      StateArray05[4] = Linköping;
   
      StateArray05[5] = Mjölby;
   
      StateArray05[6] = Motala;
   
      StateArray05[7] = Norrköping;
   
      StateArray05[8] = Söderköping;
   
      StateArray05[9] = Vadstena;
   
      StateArray05[10] = Valdemarsvik;
   
      StateArray05[11] = Ydre;
   
      StateArray05[12] = Åtvidaberg;
   
      StateArray05[13] = Ödeshög;
   

// Function to populate the area codes for the state selected
function PopulateAreaCode() {
   // Only process the function if the first item is not selected.
   if (document.StateForm.StateCode.selectedIndex != 0) {
      // Find the state abbreviation
      var ThisState = document.StateForm.StateCode[document.StateForm.StateCode.selectedIndex].value;
      // Set the length of the arecode drop down equal to the length of the state's array
      document.StateForm.AreaCode.length = eval("StateArray" + ThisState + ".length");
      // Put 'Select' as the first option in the area code drop-down
      document.StateForm.AreaCode[0].value = "";
      document.StateForm.AreaCode[0].text = "Select";
      document.StateForm.AreaCode[0].selected = true;
      // Loop through the state's array and populate the area code drop down.
      for (i=1; i<eval("StateArray" + ThisState + ".length"); i++) {
         document.StateForm.AreaCode[i].value = eval("StateArray" + ThisState + "[i]");
         document.StateForm.AreaCode[i].text = eval("StateArray" + ThisState + "[i]");
      }
   }
}
//-->
</script>

<form name="StateForm">
<p>
<table border="0">
   <tr>
      <td><b>State</b></td>
      <td><b>Area Code</b></td>
   </tr>
   <tr>
      <td>
         <select name="StateCode" onChange="PopulateAreaCode()">
            <option value="0">Select State
            
               <option value="10">Blekinge län
            
               <option value="09">Gotlands län
            
               <option value="13">Hallands län
            
               <option value="06">Jönköpings län
            
               <option value="08">Kalmar län
            
               <option value="07">Kronobergs län
            
               <option value="12">Skåne län
            
               <option value="14">Västra Götalands län
            
               <option value="05">Östergötlands län
            
         </select>
      </td>
   <td>
      <select name="AreaCode" style="width:150" size="1">
         <option value="0">Select Area Code
      </select>
   </td>
   </tr>
</table>
</p>
</form>
</body>
</html>
Peter SMedlem sedan dec. 20025 483 inlägg
#5

Du måste ha "snuffar" eller apostrofer kring varje kommunnamn.

:)

Jesper TMedlem sedan nov. 20017 144 inlägg
#6

Jag har gjort ett län/komunscript. Du hittar det här:
http://www.webforum.nu/showthread.php?s=&threadid=63304

InsiderMedlem sedan jan. 2000978 inlägg
#7

Klockrent!
Jag villade bort mig i funktionen. Önskar att man fick tydligare felmeddelanden...

Tackar!! :)

132 ms totalt · 3 externa anrop · v20260731065814-full.30151723
0 ms — hämta forumlista (cache)
0 ms — hämta statistik (cache)
129 ms — hämta tråd, inlägg och bilagor (db)