Visualizzazione dei risultati da 1 a 10 su 10
  1. #1

    Prelevare valore select, creata con uno script

    Ciao a tutti,
    ho creato uno script che mi crea dinamicamente un menù a tendina con 4 opzioni e un link che apre una popup. A questo link è collegata la funzione selezionaCliente(), a questa funzione vorrei però passare un parametro che deve essere l'option selezionato della select appena creata.
    Ho provato a scrivere in questo modo selezionaCliente('"+document.all['linea'].options[document.all['linea'].selectedIndex].value+"');, ma è come se non me lo trovasse, mentre tutti gli altri elementi del form creati in html li trova.
    Come faccio?

    Vi posto il codice.

    var select = document.createElement("select");
    select.setAttribute("name","linea");
    select.className="select";
    select.style.marginLeft="9px";

    var option1 = document.createElement("option");
    var t1 = document.createTextNode("a");
    option1.setAttribute("value","a");
    option1.appendChild(t1);
    select.appendChild(option1);

    var option2 = document.createElement("option");
    var t2 = document.createTextNode("b");
    option2.setAttribute("value","b");
    option2.appendChild(t2);
    select.appendChild(option2);

    var option3 = document.createElement("option");
    var t3 = document.createTextNode("c");
    option3.setAttribute("value","c");
    option3.appendChild(t3);
    select.appendChild(option3);

    var option4 = document.createElement("option");
    var t4 = document.createTextNode("d");
    option4.setAttribute("value","d");
    option4.appendChild(t4);
    select.appendChild(option4);

    var divUltimoCampo = document.createElement("div");
    divUltimoCampo.className="ultimoCampo";
    divUltimoCampo.setAttribute("id","ultimoCampoClien te");

    divUltimoCampo.innerHTML = "<a href=\"#\" onclick=\"selezionaCliente();\">seleziona cliente</label>";

    Grazie.

  2. #2
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    premesso che select non e' un nome da usare come variabile

    document.nomeForm.elements['linea'].options[document.nomeForm.elements['linea'].selectedIndex].value
    che poi elements non e' indispensabile, linea e' un nome accetabile x poter scrivere cosi'
    document.nomeForm.linea.options[document.nomeForm.linea.selectedIndex].value

    ciao

  3. #3
    vabbè i nomi non mi cambia nulla...però non funziona ancora, mi dice che:

    document.mioForm.linea.options[document.mioForm.linea.selectedIndex].value è nullo o non è un oggetto.

  4. #4
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    codice:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    
    <html>
    <head>
    <title></title>
    <style type="text/css">
    <!--
    
    -->
    </style>
    <script language="JavaScript" type="text/javascript">
    <!--
    function selezionaCliente(val){
    	alert(val)
    }
    
    function addSelect(){
    var select = document.createElement("select");
    select.setAttribute("name","linea");
    select.className="select";
    select.style.marginLeft="9px";
    
    var option1 = document.createElement("option");
    var t1 = document.createTextNode("a");
    option1.setAttribute("value","a");
    option1.appendChild(t1);
    select.appendChild(option1);
    
    var option2 = document.createElement("option");
    var t2 = document.createTextNode("b");
    option2.setAttribute("value","b");
    option2.appendChild(t2);
    select.appendChild(option2);
    
    var option3 = document.createElement("option");
    var t3 = document.createTextNode("c");
    option3.setAttribute("value","c");
    option3.appendChild(t3);
    select.appendChild(option3);
    
    var option4 = document.createElement("option");
    var t4 = document.createTextNode("d");
    option4.setAttribute("value","d");
    option4.appendChild(t4);
    select.appendChild(option4);
    
    var divUltimoCampo = document.createElement("div");
    divUltimoCampo.className="ultimoCampo";
    divUltimoCampo.setAttribute("id","ultimoCampoCliente");
    
    document.getElementById('mioForm').appendChild(select)
    
    
    divUltimoCampo.innerHTML = "<a href=\"#\" onclick=\"selezionaCliente(document.mioForm.linea.options[document.mioForm.linea.selectedIndex].value);\">seleziona cliente</a>";
    document.getElementById('mioForm').appendChild(divUltimoCampo)
    }
    //-->
    </script>
    </head>
    <body>
    <form name="mioForm" id="mioForm">
    <input type="button" onclick="addSelect()">
    </form>
    </body>
    </html>
    funziona

  5. #5
    A me non funziona...forse ho explorer che dà i numeri...

    Ho provato per curiosità la soluzione che ti avevo inviato prima su FF e lì funziona, ma su explorer continua a darmi il solito errore. Anche con il tuo codice...

  6. #6
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    hai ragione, con IE non funge

  7. #7
    Originariamente inviato da Xinod
    hai ragione, con IE non funge
    Quindi?? Che faccio??
    Non c'è un modo di mostrare tutti gli elementi del form, almeno per vedere se lo riconosce?

  8. #8
    Ho trovato una soluzione schifosa e temporanea.

    document.getElementById('mioForm').elements[7].value;

    Sapendo che il mio campo è il 7° allora scrivendo così me lo prende e funziona, però se trovi un'altra soluzione molto più valida ti sarei grato...al posto del 7 ho già provato con il nome, ma non ne vuole sapere...

  9. #9
    Ho trovato la soluzione, dato che il buon vecchio IE quando decide di rompere rompe, allora ho pensato: il nome ti fa schifo? bene allora proviamo con l'id...ed ecco risolto il problema!!

    Funziona anche su FF...

    Ciaoo

  10. #10
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    cmq non sei l' unico che ha problemi con IE e createElement('option')
    http://www.sitepoint.com/forums/showthread.php?t=415589
    propone add o ancora meglio il vecchio sistema new Option()

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.