• User

    [javascript] problemino con le select dinamiche

    ciao,

    ho questo JS:

    
    <script type="text/javascript">
    var shipping = new Object() 
    shipping["1"] = [{value:"1", text:"Carta"}, 
                          {value:"2", text:"Contrassegno"}, 
                          {value:"3", text:"Bollettino"}]; 
    shipping["2"] = [{value:"1", text:"Carta"}, 
                        {value:"4", text:"Bonifico"}]; 
    shipping["3"] = [{value:"1", text:"Carta"}, 
                              {value:"4", text:"Bonifico"}]; 
    
    function setCost(chooser) { 
        var newElem; 
        var where = (navigator.appName == "Microsoft Internet Explorer") ? -1 : null; 
        var costChooser = chooser.form.elements["id_pag"]; 
        while (costChooser.options.length) { 
            costChooser.remove(0); 
        } 
        var choice = chooser.options[chooser.selectedIndex].value; 
        var db = shipping[choice]; 
        newElem = document.createElement("option"); 
        newElem.text = "tipo di pagamento:"; 
        newElem.value = ""; 
        costChooser.add(newElem, where); 
        if (choice != "") { 
            for (var i = 0; i < db.length; i++) { 
                newElem = document.createElement("option"); 
                newElem.text = db*.text; 
                newElem.value = db*.value; 
                costChooser.add(newElem, where); 
            } 
        } 
    }
    </script>
    <select name="id_area" onchange="setCost(this)">
    <option value="" selected="selected">Seleziona area</option>
    
    <option value="1">italia</option>
    <option value="2">Europa</option>
    <option value="3">extraeuropa</option>
    </select>
    </td></tr>
    <tr><td colspan="8">
    <select name="id_pag" onchange="this.form.submit();">
    <option value="" selected="selected">tipo di pagamento</option>
    </select>
    
    

    funziona bene, tranne una cosa che devo assolutamente risolvere....😢

    in pratica all'onchange mi resetta le scelte che evevo effettuato per il select 1 e 2 ...
    come posso fare in modo che anche dopo il submit del form, mi rimangano selezionate le scelte?
    purtroppo non sono molto esperto di JS!
    grazie mille!!


  • Super User

    Ciao 🙂

    puoi postare una pagina online completa che presenta il problema. 🙂

    Qui mancano un po' di cose. Ad esempio non vedo il tag <form>

    :ciauz:


  • User

    ciao,
    non posso farti vedere la pagina online, ma ti ho ricostruito la parte completa funzionante e con il problema del refresh visibile. copia/incolla il codice in un file

    <form action="" method="post" name="frmCart" id="frmCart">
    <script type="text/javascript">
    
    var shipping = new Object()
    shipping["1"] = [{value:"1", text:"Carta di credito"},
                          {value:"2", text:"contrassegno"},
                          {value:"3", text:"Bollettino postale"}];
    shipping["2"] = [{value:"1", text:"Carta di credito"},
                          {value:"4", text:"bonifico bancario"}];
    shipping["3"] = [{value:"1", text:"Carta di credito"},
                          {value:"4", text:"bonifico bancario"}];
    
    function setCost(chooser) {
        var newElem;
        var where = (navigator.appName == "Microsoft Internet Explorer") ? -1 : null;
        var costChooser = chooser.form.elements["id_pag"];
        while (costChooser.options.length) {
            costChooser.remove(0);
        }
        var choice = chooser.options[chooser.selectedIndex].value;
        var db = shipping[choice];
        newElem = document.createElement("option");
        newElem.text = "tipo di pagamento:";
        newElem.value = "";
        costChooser.add(newElem, where);
        if (choice != "") {
            for (var i = 0; i < db.length; i++) {
                newElem = document.createElement("option");
                newElem.text = db*.text;
                newElem.value = db*.value;
                costChooser.add(newElem, where);
            }
        }
    }
    
    </script>
    
    <select name="id_area" onchange="setCost(this)">
    <option value="" selected="selected">Seleziona un area</option>
    
    <option value="1">Italia</option>
    <option value="2">Europa e bacino del Mediterraneo</option>
    <option value="3">Paesi extra europei</option>
    </select>
    </td></tr>
    <tr><td colspan="8">
    <select name="id_pag" onchange="this.form.submit();">
    <option value="" selected="selected">Seleziona il tipo di pagamento</option>
    </select>
    
    </form>
    

    grazie


  • Super User

    Ciao 🙂

    Grazie di avermi chiarito meglio la situazione.

    L'unico modo per ovviare penso sia quello di memorizzare le variabili tramite php, o tramite url e poi ripassarle al form.

    Infatti dopo un submit viene caricata una nuova pagina, cioè quella che compie l'azione (in questo caso non ancora specificata).

    Poi in quella pagina bisogna recuperare le scelte fatte. Il metodo più diffuso è creando un form dinamicamente con php e con valori predefiniti pari a quelli precedentemente scelti.

    Ciao :ciauz:


  • User Newbie

    ciao, ho popolato la mia pagina con php da un DB. il risultato e' praticamente uguale a quello discusso nel thread.
    solo che non funziona e non riesco capire dove sbaglio. aiuto..
    vi posto il codice:

    
    
    <script type="text/javascript">
    var dati = new Objetc() 
    dati["2010-06-26"] = [ {value:"0", text:"08:00:00"} ];
    dati["2010-06-27"] = [ {value:"1", text:"08:20:00"}, {value:"2", text:"08:00:00"} ];
    dati["2010-06-28"] = [ {value:"3", text:"08:20:00"} ];
    dati["2010-08-28"] = [ {value:"4", text:"21:20:00"} ];
    
    function setSlot(chooser) {
        var newElem;
        var where = (navigator.appName == "Microsoft Internet Explorer") ? -1 :    null; 
        var slotChooser = chooser.form.elements["slot"];
        while (slotChooser.options.length) {
            slotChooser.remove(0);
        }
        var choice = chooser.options[chooser.selectedIndex].value;
        var db = dati[choice];
        newElem = document.createElement("option");
        newElem.text = "seleziona uno slot:";
        newElem.value = "";
        slotChooser.add(newElem, where);
        if (choice != "") {
            for (var i = 0; i < db.length; i++) {
                newElem = document.createElement("option");
                newElem.text = db*.text;
                newElem.value = db*.value;
                slotChooser.add(newElem, where);
            }
        }
    }
      
    
    </script>
    <table>
     <form action="" method="post" name="frmCart" id="frmCart">
    Data:
    <select name="data" onchange="setSlot(this)">
    <option value="" selected="selected">data</option>
    <option value="2010-06-26">2010-06-26</option>
    <option value="2010-06-27">2010-06-27</option>
    <option value="2010-06-27">2010-06-27</option>
    <option value="2010-06-28">2010-06-28</option>
    <option value="2010-08-28">2010-08-28</option>
    
    </select>
    </td></tr>
    <tr><td colspan="8">
    Slot:
    <select name="slot" onchange="this.form.submit();">
    <option value="" selected="selected">Seleziona uno slot</option>
    </select>
    </form>
    </table>