2013-10-13 3 views
1

Je travaille depuis des heures pour essayer de faire fonctionner ça. Ce que je veux faire est de: Tout d'abord, ayez deux menu déroulant, le premier contrôlant le second. Ensuite, je veux être en mesure de relier chaque "seconde sélection" individuelle à un href. Cependant, les "secondes sélections" sont dans un tableau et je ne sais pas comment y accéder en html. S'il vous plaît aider.Comment attacher des liens à des éléments de tableau individuels?

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
     "http://www.w3.org/TR/html4/loose.dtd"> 
    <html> 
    <head> 
    <title>Any Title</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 

    <script type="text/javascript"> 

     var firmwares = []; 
     firmwares[1] = ["Sacramento","San Diego","San Francisco","Los Angeles"]; 
     firmwares[2] = ["Cleveland","Akron","Canton","Cincinnati","Columbus"]; 
     firmwares[3] = ["Philadelphia","Pittsburgh","Harrisburgh"]; 
     firmwares[4] = []; 

     function fillSelect(nValue,nList){ 

      nList.options.length = 1; 
      var curr = firmwares[nValue]; 
      for (each in curr) 
       { 
       var nOption = document.createElement('option'); 
       nOption.appendChild(document.createTextNode(curr[each])); 
       nOption.setAttribute("value",curr[each]);   
       nList.appendChild(nOption); 
       } 
     } 
    </script> 

    </head> 
    <body> 
     <form method="post" action=""> 
      <div> 
       <select name='phones' onchange="fillSelect(this.value,this.form['firmwares'])"> 
        <option value="">Select Your State</option> 
        <option value="1">California</option> 
        <option value="2">Ohio</option> 
        <option value="3">Pennsylvania</option> 
        <option value="4">Alaska</option> 
       </select> 
       <select name='firmwares' > 
        <option value="">Select Your City</option> 
       </select> 
      </div> 
     </form> 
    </body> 
    </html> 
+0

Si je vous ai bien compris, vous voulez montrer une seconde liste déroulante basée sur la sélection de menu déroulant précédent? Cochez cette réponse: [link] (http://stackoverflow.com/questions/6954556/show-a-second-dropdown-based-on-previous-dropdown-selection) – WhatisSober

Répondre

0

Essayez cette

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<title>Any Title</title> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 

<script type="text/javascript"> 

    var firmwares = {}; 
    firmwares[1] = {"Sacramento" : "http://www.abc.com", 
        "San Diego" : "http://www.abc.com", 
        "San Francisco" : "http://www.abc.com", 
        "Los Angeles" : "http://www.abc.com"}; 
    firmwares[2] = {"Cleveland" : "http://www.abc.com", 
        "Akron" : "http://www.abc.com", 
        "Canton" : "http://www.abc.com", 
        "Cincinnati" : "http://www.abc.com", 
        "Columbus" : "http://www.abc.com"}; 
    firmwares[3] = {"Philadelphia" : "http://www.abc.com", 
        "Pittsburgh" : "http://www.abc.com", 
        "Harrisburgh" : "http://www.abc.com"}; 
    firmwares[4] = {}; 

    function fillSelect(nValue,nList){ 

     nList.options.length = 1; 
     var curr = firmwares[nValue]; 
     for (each in curr) 
      { 
      var nOption = document.createElement('option'); 
      nOption.appendChild(document.createTextNode(each)); 
      nOption.setAttribute("value",each);   
      nList.appendChild(nOption); 
      } 

     document.getElementById("gotoCity").style.display = "none"; 
    } 

    function openCity(firmware, city) { 
     var gotoCity = document.getElementById("gotoCity"); 
     gotoCity.href = firmwares[firmware][city]; 
     gotoCity.style.display=""; 
    } 
</script> 

</head> 
<body> 
    <form method="post" action=""> 
     <div> 
      <select name='phones' onchange="fillSelect(this.value,this.form['firmwares'])"> 
       <option value="">Select Your State</option> 
       <option value="1">California</option> 
       <option value="2">Ohio</option> 
       <option value="3">Pennsylvania</option> 
       <option value="4">Alaska</option> 
      </select> 
      <select name='firmwares' onchange="openCity(this.form['phones'].value, this.value)" > 
       <option value="">Select Your City</option> 
      </select> 
      <a id="gotoCity" href="#" style="display:none;">Go</href> 
     </div> 
    </form> 
</body> 
</html> 
+0

Merci! Ça a marché! Y at-il un moyen de faire fonctionner les liens à la commande d'un bouton au lieu d'automatique? Encore une fois, merci. – umtek12

+0

@ umtek12 changez le code que vous voulez. J'espère que cela fonctionne pour vous. – Cary

+0

Merci encore !!!!!!!!!!!!! – umtek12

Questions connexes