View Single Post
Old 11-20-2012, 11:29 PM   PM User | #12
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,763
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Lightbulb

Be sure to change the 'baseURL' to your common site or you'll get a lot of 'file not found' errors.
Code:
<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8" />
<title> Untitled </title>

<script type="text/javascript">

// NOTE: You need to fill in the links as defined by your requirements in the following array elements
var provincias = [
  [],  // spare (not used in this version of program)
  ['-|-','Almería|Almería',
         'Cádiz|Cádiz',
         'Córdoba|Córdoba',
         'Granada|Granada',
         'Huelva|Huelva',
         'Jaén|Jaén',
         'Málaga|Málaga',
         'Sevilla|Sevilla'],
  ['-|-','Huesca|Huesca',
         'Teruel|Teruel',
         'Zaragoza|Zaragoza'],
  ['-|-','Asturias|Asturias'],
  ['-|-','Baleares|Baleares']  // Note: no comma after final entry
];

function cambiar(formulario){
  var i = 0;
  var select1 = formulario['D1'];
  var select2 = formulario['D2'];
  var vector = provincias[select1.selectedIndex];
  if(vector.length)select2.length=vector.length;
  var tarr = [];
  while(vector[i]){
    tarr = vector[i].split('|');
    select2.options[i].value = tarr[1];
    select2.options[i].text = tarr[0];
    i++;
  }
  select2.options[0].selected = 1;
}
function goToSite(info) {
  if (info != '-') { alert(info); }                       // this line is for testing purposes only
//  if (info != '-') { document.location.href = info; }   // uncomment this line after testing
}

function linkToSite(formulario) {
  var select1 = formulario['D1'];
  var select2 = formulario['D2'];
  var baseURL = 'http://www.codingforums.com/';  // defined as some common URL if same for all links
  var str = '';
  str = baseURL+select1.value+select2.value+'.html';
  goToSite(str); 
}
</script>

<style type="text/css">

</style>
</head>
<body>
<form method="POST">
  <select name="D1" onchange="cambiar(this.form)">
  <option>-</option>
  <option>Andalucía</option>
  <option>Aragón</option>
  <option>Asturias</option>
  <option>Baleares</option>
  </select>
  <select name="D2" onchange="linkToSite(this.form)">
  </select></p>
</form>
</body>
</html>
Could be simplified even more with you last requirement,
but not worth the effort if you are happy with the results.
jmrker is offline   Reply With Quote
Users who have thanked jmrker for this post:
linkysnake (11-21-2012)