Originariamente inviato da the darkness
mi togli una curiosità?ma ti sei messo a fare a mano per tutti i comuni???
Io ho un amico che si è prestato molto volentieri a farmelo
. Si chiama asp.net
per chi fosse interessanto, invio una pagina ridotta. Nel server si ricava il vettore bidimensionale province e comuni che ho ridotto per spazio a 3.
Il vettore province contiene id_provincia, nome_provincia, id_regione. Il vettore comuni contiene id_comune, nome_comune e id_provincia.
quando si seleziona una regione, si ricava l'id_regione e si recuperano tutte le province per quel id_regione...
codice:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Pagina senza titolo </title>
<link href="../stili/Styles.css" rel="stylesheet" type="text/css" />
<script language="javascript" type="text/javascript">
// <!CDATA[
var province = [ ["084", "AGRIGENTO", "19"], ["006", "ALESSANDRIA", "01"], ["042", "ANCONA", "11"] ];
var comuni = [ ["28001", "ABANO TERME", "028"], ["98001", "ABBADIA CERRETO", "098"], ["97001", "ABBADIA LARIANA", "097"] ];
function DropDownList_regioni_onchange(v)
{
//chiave regione
var key = v.value;
//cancello tutti le province
$("DropDownList_province").options.length = 1;
//cancello tutti i comuni
$("DropDownList_comuni").options.length = 1;
//creo lista provincia
for(var i in province)
{
if(province[i][2] == key)
{
lista_push($("DropDownList_province"), province[i][1], province[i][0]);
}
}
}
function DropDownList_province_onchange(v)
{
//chiave provincia
var key = v.value;
//cancello tutti i comuni
$("DropDownList_comuni").options.length = 1;
//creo lista comuni
for(var i in comuni)
{
if(comuni[i][2] == key)
{
lista_push($("DropDownList_comuni"), comuni[i][1], comuni[i][0]);
}
}
}
function DropDownList_comuni_onchange(v)
{
}
//al posto di mettere document.getElementById("div1"), mettere $("div1")
//da prototype.js
function $()
{
var elements = new Array();
for (var i = 0; i < arguments.length; i++)
{
var element = arguments[i];
if (typeof element == 'string')
element = document.getElementById(element);
if (arguments.length == 1)
return element;
elements.push(element);
}
return elements;
}
function lista_push(lista, testo, valore)
{
lista.options[lista.options.length] = new Option(testo, valore);
}
// ]]>
</script>
</head>
<body>
<form name="form1" method="post" action="a.aspx" id="form1" >
<table>
<tr><td>Regioni</td><td>Province</td><td>Comuni</td></tr>
<tr>
<td>
<select name="DropDownList_regioni" id="DropDownList_regioni" style="width:200px;" onchange="return DropDownList_regioni_onchange(this)">
<option value=""></option>
<option value="13">ABRUZZO</option>
<option value="17">BASILICATA</option>
<option value="18">CALABRIA</option>
<option value="15">CAMPANIA</option>
<option value="08">EMILIA-ROMAGNA</option>
<option value="06">FRIULI-VENEZIA GIULIA</option>
<option value="12">LAZIO</option>
<option value="07">LIGURIA</option>
<option value="03">LOMBARDIA</option>
<option value="11">MARCHE</option>
<option value="14">MOLISE</option>
<option value="01">PIEMONTE</option>
<option value="16">PUGLIA</option>
<option value="20">SARDEGNA</option>
<option value="19">SICILIA</option>
<option value="09">TOSCANA</option>
<option value="04">TRENTINO-ALTO ADIGE</option>
<option value="10">UMBRIA</option>
<option value="02">VALLE D'AOSTA</option>
<option value="05">VENETO</option>
</select>
</td>
<td>
<select name="DropDownList_province" id="DropDownList_province" style="width:200px;" onchange="return DropDownList_province_onchange(this)">
<option value=""></option>
</select>
</td>
<td>
<select name="DropDownList_comuni" id="DropDownList_comuni" style="width:200px;" onchange="return DropDownList_comuni_onchange(this)">
<option value=""></option>
</select>
</td>
</tr>
</table>
</form>
</body>
</html>