Visualizzazione dei risultati da 1 a 6 su 6
  1. #1
    Utente di HTML.it
    Registrato dal
    Mar 2003
    Messaggi
    103

    Aiuto con script select dinamico

    alur, lo script è questo:
    codice:
    <script type="text/javascript">
    /* 
         Script  tratto dal libro "JavaScript and DHTML Cookbook" - Capitolo 8-13
         Pubblicato da O'Reilly & Associates
         Copyright 2003 Danny Goodman
    	  Riprodurre questa nota per qualunque riutilizzo del codice.
    	*/
    var regiondb = new Object()
    regiondb["africa"] = [{value:"102", text:"Cairo"},
                          {value:"88", text:"Lagos"},
                          {value:"80", text:"Nairobi"},
                          {value:"55", text:"Pretoria"}];
    regiondb["asia"] = [{value:"30", text:"Ankara"},
                        {value:"21", text:"Bangkok"},
                        {value:"49", text:"Pechino"},
                        {value:"76", text:"New Delhi"},
                        {value:"14", text:"Tokyo"}];
    regiondb["australia"] = [{value:"64", text:"Suva"},
                              {value:"12", text:"Sydney"}];
    regiondb["europa"] = [{value:"11", text:"Atene"},
                          {value:"35", text:"Francoforte"},
                          {value:"3", text:"Londra"},
                          {value:"15", text:"Madrid"},
                          {value:"1", text:"Parigi"},
                          {value:"10", text:"Roma"},
                          {value:"6", text:"Stoccolma"},
                          {value:"97", text:"San Pietroburgo"}];
    regiondb["noamer"] = [{value:"73", text:"Dallas"},
                          {value:"71", text:"Los Angeles"},
                          {value:"5", text:"New York"},
                          {value:"37", text:"Toronto"}];
    regiondb["suamer"] = [{value:"65", text:"Buenos Aires"},
                          {value:"31", text:"Caracas"},
                          {value:"66", text:"Rio di Janeiro"}];
    
    function setCities(chooser) {
        var newElem;
        var where = (navigator.appName == "Microsoft Internet Explorer") ? -1 : null;
        var cityChooser = chooser.form.elements["città"];
        while (cityChooser.options.length) {
            cityChooser.remove(0);
        }
        var choice = chooser.options[chooser.selectedIndex].value;
        var db = regiondb[choice];
        newElem = document.createElement("option");
        newElem.text = "Seleziona una città:";
        newElem.value = "";
        cityChooser.add(newElem, where);
        if (choice != "") {
            for (var i = 0; i < db.length; i++) {
                newElem = document.createElement("option");
                newElem.text = db[i].text;
                newElem.value = db[i].value;
                cityChooser.add(newElem, where);
            }
        }
    }
    
    
    </script>
    preso proprio da html.it

    Lo sto riutilizzando per fare un select dinamico di scelta dei giorni del mese(30 o 31).

    in teoria, visto che gli oggetti sono 2, uno con 30, e l'altro con 31 giorni, come posso fa si che lo stesso oggetto popoli il select se vengono selezionati diversi mesi?

    io ho provato così, ma come temevo, nulla..:
    codice:
    var giornidb = new Object()
    giornidb["Aprile 2005","Giugno 2005","Settembre 2005"] = [{value:"1", text:"1"},
    {value:"2", text:"2"},
    {value:"3", text:"3"},
    {value:"4", text:"4"},
    {value:"5", text:"5"},
    {value:"6", text:"6"},
    {value:"7", text:"7"},
    {value:"8", text:"8"},
    {value:"9", text:"9"},
    {value:"10", text:"10"},
    {value:"11", text:"11"},
    {value:"12", text:"12"},
    {value:"13", text:"13"},
    {value:"14", text:"14"},
    {value:"15", text:"15"},
    {value:"16", text:"16"},
    {value:"17", text:"17"},
    {value:"18", text:"18"},
    {value:"19", text:"19"},
    {value:"20", text:"20"},
    {value:"21", text:"21"},
    {value:"22", text:"22"},
    {value:"23", text:"23"},
    {value:"24", text:"24"},
    {value:"25", text:"25"},
    {value:"26", text:"26"},
    {value:"27", text:"27"},
    {value:"28", text:"28"},
    {value:"29", text:"29"},
    {value:"30", text:"30"}];
    giornidb["Maggio 2005","Luglio 2005","Agosto 2005","Ottobre 2005"] = [{value:"1", text:"1"},
    {value:"2", text:"2"},
    {value:"3", text:"3"},
    {value:"4", text:"4"},
    {value:"5", text:"5"},
    {value:"6", text:"6"},
    {value:"7", text:"7"},
    {value:"8", text:"8"},
    {value:"9", text:"9"},
    {value:"10", text:"10"},
    {value:"11", text:"11"},
    {value:"12", text:"12"},
    {value:"13", text:"13"},
    {value:"14", text:"14"},
    {value:"15", text:"15"},
    {value:"16", text:"16"},
    {value:"17", text:"17"},
    {value:"18", text:"18"},
    {value:"19", text:"19"},
    {value:"20", text:"20"},
    {value:"21", text:"21"},
    {value:"22", text:"22"},
    {value:"23", text:"23"},
    {value:"24", text:"24"},
    {value:"25", text:"25"},
    {value:"26", text:"26"},
    {value:"27", text:"27"},
    {value:"28", text:"28"},
    {value:"29", text:"29"},
    {value:"30", text:"30"},
    {value:"30", text:"31"}];                
    
    function setGiorni1(chooser) {
        if (
    	var newElem;
        var where = (navigator.appName == "Microsoft Internet Explorer") ? -1 : null;
        var dayChooser = chooser.form.elements[tipo];
        while (dayChooser.options.length) {
            dayChooser.remove(0);
        }
        var choice = chooser.options[chooser.selectedIndex].value;
        var db = giornidb[choice];
       
        if (choice != "") {
            for (var i = 0; i < db.length; i++) {
                newElem = document.createElement("option");
                newElem.text = db[i].text;
                newElem.value = db[i].value;
                dayChooser.add(newElem, where);
            }
        }
    }
    
    
    </script>
    Mi date una mano?

  2. #2
    Utente di HTML.it L'avatar di badaze
    Registrato dal
    Jun 2002
    residenza
    Lyon
    Messaggi
    5,372
    Cosa vuoi fare di preciso ?

    Comunque prova questo.

    codice:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    
    <html>
    <head>
    <title>Untitled</title>
    <script language="JavaScript" type="text/javascript">
    <!--
    //----------------------------------------------------
    // variabili globali
    //----------------------------------------------------
    var globalYear  = 0;
    var globalmonth = 0;
    var globalDay   = 0;
    var mese        = new Array('Gen','Feb','Mar','Apr','Mag','Giu','Lug','Ago','Set','Ott','Nov','Dic');
    //----------------------------------------------------
    // Gestione degli anni
    //----------------------------------------------------
    function years(aYear) {
     if (aYear == null) {
      var today   = new Date();
    	currentYear = today.getFullYear();
      //------------ Assegnazione alla variabile globale
      globalYear = currentYear;	
     } else {
      currentYear = aYear;
      //------------ Assegnazione alla variabile globale
      globalYear  = aYear;
    	//------------ Inizializzazione mesi		
    	months(1);	
    	return;
     } // if (aYear == null)
     //---------- Fino a 50 anni prima dell'anno in corso
     startYear = currentYear - 50;
     //---------- Fino a 5 anni dopo dell'anno in corso
     endYear   = currentYear + 5;
     //---------- Riempimento del combo box "anno"
     for (i=startYear;i<=endYear;i++) {
      //----------- creazione di una nuova option
    	var newOption = document.createElement('OPTION');
    	//----------- Assegnazione della nuova opzione al combo
    	document.getElementById('anno').add(newOption);
    	//----------- Assegnazione del valore e del testo 
    	newOption.text  = i;
    	newOption.value = i;
    	//----------- Se l'anno in corso = a quello corrente
    	if (i == currentYear) {
    	  newOption.selected = true;
    	} // if (i == currentYear)
     } // for (i=startYear;i<=endYear;i++)
     if (aYear != null) {
    	//------------ Inizializzazione mesi		
    	months(1);	
    	return;
     } // if (aYear != null) 
    } // function years(aYear)
    
    //----------------------------------------------------
    // Gestione dei mesi
    //----------------------------------------------------
    function months(aMonth) {
     if (aMonth == null) {
      //------------ Mese in corso
      var today    = new Date();
      //------------ Ricavare il mese in corso + 1 perché Gennaio = 0
      currentMonth = today.getMonth() + 1; 
      //------------ Assegnazione alla variabile globale
      globalMonth = currentMonth;	
     } else {
      currentMonth = aMonth;
      //------------ Assegnazione alla variabile globale
      globalMonth  = aMonth;	
     } // if (aMonth == null) 
     //------------ Cancellazione dei dati già presenti
     document.getElementById('mese').innerHTML = ''; 
     //------------ In ogni anno ci sono sempre 12 mesi
     for (i=1;i<=12;i++) {
      //----------- creazione di una nuova option
    	var newOption = document.createElement('OPTION');
    	//----------- Assegnazione della nuova opzione al combo
    	document.getElementById('mese').add(newOption);
    	//----------- Assegnazione del valore e del testo 
    	newOption.text  = mese[i-1];
    	newOption.value = i;
    	//----------- Se il mese in corso = a quello corrente
    	if (i == currentMonth) {
    	  newOption.selected = true;
    	} // if (i == currentMonth) 
     } // for (i=1;i<=12;i++)  
     if (aMonth != null) { 
    	//------------ Inizializzazione giorni	
    	days(1);
     } // if (aMonth != null) 
    } // function months()
    
    //----------------------------------------------------
    // Gestione dei giorni
    //----------------------------------------------------
    function days(aDay) {
     //------------ Giorno in corso
     if (aDay == null) {
      var today    = new Date();
      //------------ Ricavare il mese in corso + 1 perché Gennaio = 0
      currentDay = today.getDate();
     } else {
      currentDay = aDay;
     } // if (aDay == null)  
     //------------ Cancellazione dei dati già presenti
     document.getElementById('giorno').innerHTML = '';
     //------------ In ogni mese ci sono almeno 28 giorni
     for (i=1;i<=28;i++) {
      //----------- creazione di una nuova option
    	var newOption = document.createElement('OPTION');
    	//----------- Assegnazione della nuova opzione al combo
    	document.getElementById('giorno').add(newOption);
    	//----------- Assegnazione del valore e del testo 
    	newOption.text  = i;
    	newOption.value = i;
    	//----------- Se il mese in corso = a quello corrente
    	if (i == currentDay) {
    	  newOption.selected = true;
    	} // if (i == currentDay)  
     } // for (i=1;i<=28;i++)
     //------------ Il mese di febbraio puo' avere 29 giorni
     if (globalMonth == 2) {
      //------------ Se è un anno bissestile - ATTENTO ci sono altri requisiti !!!!
    	if ((globalYear/4) == parseInt(globalYear/4)) {
       //----------- creazione di una nuova option
       var newOption = document.createElement('OPTION');	
    	 //----------- Assegnazione della nuova opzione al combo 
    	 document.getElementById('giorno').add(newOption);
    	 //----------- Assegnazione del valore e del testo 
    	 newOption.text  = 29;
    	 newOption.value = 29;
    	 //----------- Se il mese in corso = a quello corrente
    	 if (currentDay == 29) {
    	  newOption.selected = true;
    	 } // 	if (currentDay == 29)
    	} // if ((globalYear/4) == parseInt(globalYear/4))
     } else {
      //----------- Tutti i mesi hanno almeno 30 giorni
      //----------- creazione di una nuova option
      var newOption = document.createElement('OPTION');		
    	//----------- Assegnazione della nuova opzione al combo per il giorno 29
    	document.getElementById('giorno').add(newOption);
    	//----------- Assegnazione del valore e del testo 
    	newOption.text  = 29;
    	newOption.value = 29;
    	//----------- Se il mese in corso = a quello corrente
    	if (currentDay == 29) {
    	  newOption.selected = true;
    	} // 	if (currentDay == 29)
      //----------- creazione di una nuova option
      var newOption = document.createElement('OPTION');		
    	//----------- Assegnazione della nuova opzione al combo il giorno 30
    	document.getElementById('giorno').add(newOption);
    	//----------- Assegnazione del valore e del testo 
    	newOption.text  = 30;
    	newOption.value = 30;
    	//----------- Se il mese in corso = a quello corrente
    	if (currentDay == 30) {
    	  newOption.selected = true;
    	} // 	if (currentDay == 30)		
    	//----------- Alcuni mesi hanno 31 giorni
    	if (globalMonth != 4 && globalMonth != 6 && globalMonth != 9 && globalMonth != 11) {  
       //----------- creazione di una nuova option
       var newOption = document.createElement('OPTION');		
    	 //----------- Assegnazione della nuova opzione al combo il giorno 31
    	 document.getElementById('giorno').add(newOption);
    	 //----------- Assegnazione del valore e del testo 
    	 newOption.text  = 31;
    	 newOption.value = 31;
    	 //----------- Se il mese in corso = a quello corrente
    	 if (currentDay == 31) {
    	  newOption.selected = true;
    	 } // 	if (currentDay == 30)		
    	} // if (globalMonth != 4 && globalMonth != 6 && globalMonth != 9 && globalMonth != 11)
     } // if (globalMonth == 2)
     //------------ Assegnazione alla variabile globale
     globalDay = currentDay; 
    } // function days() 
    //-->
    </script>
    
    
    </head>
    <body>
    Giorno <select id="giorno" ></select>
    Mese   <select id="mese"   onchange="months(this.value);"></select>
    Anno   <select id="anno"   onchange="years(this.value);"></select>
    
    <script language="JavaScript" type="text/javascript">
    <!--
    years();
    months();
    days();
    //-->
    </script>
    
    </body>
    </html>

  3. #3
    Utente di HTML.it
    Registrato dal
    Mar 2003
    Messaggi
    103
    purtroppo il select è così:

    codice:
    <select class="form_info1" name="dal_mese" onchange="setGiorni1(this)">
    											<option value="" <? if($dal_mese == '') print "selected" ?>>Seleziona mese</option>
                                                <option value="Aprile 2005" <? if($dal_mese == 'Aprile 2005') print "selected" ?>>Aprile 2005</option>
                                                <option value="Maggio 2005"<? if($dal_mese == 'Maggio 2005') print "selected" ?>>Maggio 2005</option>
                                                <option value="Giugno 2005"<? if($dal_mese == 'Giugno 2005') print "selected" ?>>Giugno 2005</option>
                                                <option value="Luglio 2005"<? if($dal_mese == 'Luglio 2005') print "selected" ?>>Luglio 2005</option>
                                                <option value="Agosto 2005"<? if($dal_mese == 'Agosto 2005') print "selected" ?>>Agosto 2005</option>
                                                <option value="Settembre 2005"<? if($dal_mese == 'Settembre 2005') print "selected" ?>>Settembre 2005</option>
                                                <option value="Ottobre 2005"<? if($dal_mese == 'Ottobre 2005') print "selected" ?>>Ottobre 2005</option>
    </select>_
    <select class="form_info1" name="dal_giorno">
    
    </select>
    quindi non credo potrò utilizzare lo script che mi hai fornito, vorrei sapere se nell'oggetto
    codice:
    giornidb["Aprile 2005","Giugno 2005","Settembre 2005"] = [{value:"1", text:"1"},
    {value:"2", text:"2"},
    {value:"3", text:"3"},
    {value:"4", text:"4"},
    {value:"5", text:"5"},
    {value:"6", text:"6"},
    {value:"7", text:"7"},
    {value:"8", text:"8"},
    {value:"9", text:"9"},
    {value:"10", text:"10"},
    {value:"11", text:"11"},
    {value:"12", text:"12"},
    {value:"13", text:"13"},
    {value:"14", text:"14"},
    {value:"15", text:"15"},
    {value:"16", text:"16"},
    {value:"17", text:"17"},
    {value:"18", text:"18"},
    {value:"19", text:"19"},
    {value:"20", text:"20"},
    {value:"21", text:"21"},
    {value:"22", text:"22"},
    {value:"23", text:"23"},
    {value:"24", text:"24"},
    {value:"25", text:"25"},
    {value:"26", text:"26"},
    {value:"27", text:"27"},
    {value:"28", text:"28"},
    {value:"29", text:"29"},
    {value:"30", text:"30"}];
    posso inserire i tre mesi anzi che uno solo perchè così come l'ho scritto non funziona, e mi toccherebbe fare un oggetto per ogni mese(facendo quindi dei duplicati)

  4. #4
    Moderatore di JavaScript L'avatar di br1
    Registrato dal
    Jul 1999
    Messaggi
    19,998
    Mi sembra una complicazione eccessiva per un problema cosi' semplice... ti propongo un'alternativa, non ottimmizzata perche' didattica, ma utilizzabile.

    codice:
    <HEAD>
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function popola_gg(mese) {
    	var newData = new Date()
    	anno = newData.getFullYear(); // anno attuale
    	var newData = new Date(anno,mese,0); // l'ultimo del mese richiesto
    	var maxg = newData.getDate();
    	g = document.mioForm.gg;
    	g.options.length=0;
    	for (var i=0;i<maxg;i++) {
    		g.options[i] = new Option(i+1,i+1)
    	}
    }
    //-->
    </SCRIPT>
    </HEAD>
    <BODY onload="popola_gg(1);">
    <form name="mioForm">
    <select name="mm" onchange="popola_gg(this.options[this.selectedIndex].value);">
    <option value=1>Gennaio</option>
    <option value=2>Febbraio</option>
    <option value=3>Marzo</option>
    <option value=4>Aprile</option>
    ...
    <option value=11>Novembre</option>
    <option value=12>Dicembre</option>
    </select>
    <select name="gg">
    </select>
    </form>
    </BODY>
    Il guaio per i poveri computers e' che sono gli uomini a comandarli.

    Attenzione ai titoli delle discussioni: (ri)leggete il regolamento
    Consultate la discussione in rilievo: script / discussioni utili
    Usate la funzione di Ricerca del Forum

  5. #5
    Utente di HTML.it L'avatar di badaze
    Registrato dal
    Jun 2002
    residenza
    Lyon
    Messaggi
    5,372
    Ma a cosa serve fare una select valida solo x un anno ?

    Prova comunque il mio codice (gestisce anche gli anni bissestili - anche se la formula nn è del tutto completa)

  6. #6
    Utente di HTML.it
    Registrato dal
    Mar 2003
    Messaggi
    103
    è perfetto!! grazie mille!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.