2012-11-21 27 views
5

Ho creato un modulo HTML, in questo modulo quando faccio clic su un pulsante per creare campi di testo di input in base a un criterio predefinito, questo funziona correttamente. ora quando provo a recuperare il valore inserito in quei campi di testo creati usando l'avviso non sono in grado di farlo.Recupero del valore degli elementi creati dinamicamente

Ho due domande

  1. Qual è il modo migliore per recuperare gli ingressi dai campi di testo creati in modo dinamico?
  2. mi puoi dire perché il codice che ho scritto non funziona

codice HTML

<BODY> 
<FORM> 
    <BR/> 
    <div align = "center"> 
     <br /><br /> 
     <INPUT type="button" value="Click To Enter Values" onclick="getkeywords()"/> 
    </div> 
    <div align="center" id="d_div"> 
     <form name="permavalues" id="d_form"> 
     </form> 
     <br/> <br/> 
    </div> 
</FORM> 

il codice Javascript che sto usando è questo:

function getkeywords() { 
    var index_array = new Array(); 
    var myString = "one and a two and a three = $ and four = $ and five = $"; 
    var splitresult = myString.split(" "); 

    for(i = 0; i < splitresult.length; i++) 
    { 
     if (splitresult[i] == "$" && i > 1) //retireving the keywords.. 
     { 
      add(splitresult[i-2]); 
     } 
    } 
} 

La funzione di aggiunta chiamata in getkeywords:

function add(s) { 
    //Create an input type dynamically. 
    var element = document.createElement("input"); 
    //Assign different attributes to the element. 
    element.setAttribute("type", "text"); 
    element.setAttribute("value", s); 
    element.setAttribute("name", s); 
    element.setAttribute("id", s); 

    var foo = document.getElementById("d_form"); 

    //Append the element in page (in span). 
    foo.appendChild(element); 
    alert("Value=" + document.getElemebtById(s).value); 
} 

penso che devo avere un errore con element.setAtrribute("id",s);

+0

Hai un errore di battitura nel codice: 'alert ("value =" + document.getElemebtById (s) .value);' 'deve essere alert ("Value =" + document.getElementById (s). valore); ' – IvanL

+0

oh, devo perderlo ... è la logica corretta sono più interessato alla logica che ho usato. qui sto impostando l'attributo value su s, tuttavia quando i testi sono visualizzati nella pagina web e gli utenti enset qualche input, come posso recuperarlo?sarà memorizzato nella proprietà del negozio? – Riddle

+0

È sempre possibile recuperare il valore immesso dall'utente nello stesso modo in cui lo si sta visualizzando in questo momento. Seleziona l'elemento per id e recupera il valore. 'Document.getElementById (s) .value'. Se usi jQuery, alla fine puoi eseguire tutti gli input per elaborarli, ma ciò ovviamente dipende da cosa hai intenzione di fare con la tua pagina. – IvanL

risposta

0

Il problema principale è che non si può mettere modulo all'interno di un altro modulo. Si prega di rimuovere la riga di codice HTML 2 e riga 13.

Un altro problema è il tuo errore di battitura, come ha detto IvanL.

Altro codice va bene.

Fornire codice di lavoro completamente testato come di seguito.

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
<title>test</title> 
<script language="javascript"> 
function getkeywords() { 
    var index_array = new Array(); 
    var myString = "one and a two and a three = $ and four = $ and five = $"; 
    var splitresult = myString.split(" "); 

    for(i = 0; i < splitresult.length; i++) 
    { 
     if (splitresult[i] == "$" && i > 1) //retireving the keywords.. 
     { 
      add(splitresult[i-2]); 
     } 
    } 
} 
function add(s) { 
    //Create an input type dynamically. 
    var element = document.createElement("input"); 
    //Assign different attributes to the element. 
    element.setAttribute("type", "text"); 
    element.setAttribute("value", s); 
    element.setAttribute("name", s); 
    element.setAttribute("id", s); 

    var foo = document.getElementById("d_form"); 

    //Append the element in page (in span). 
    foo.appendChild(element); 
    alert("Value=" + document.getElementById(s).value); 
} 
</script> 
</head> 
<BODY> 
    <BR/> 
    <div align = "center"> 
     <br /><br /> 
     <INPUT type="button" value="Click To Enter Values" onclick="getkeywords()"/> 
     <br><br><br> 
     <input type="button" value="add" onclick="add('tt')"> 
    </div> 
    <div align="center" id="d_div"> 
     <form name="permavalues" id="d_form"> 
     </form> 
     <br/> <br/> 
    </div> 
</body> 
</html> 
0

Qual è il modo migliore per recuperare gli ingressi dai campi di testo creati in modo dinamico?

Vorrei utilizzare JQuery per attraversare il modulo per tutti gli elementi di input di testo e recuperare i rispettivi valori.

In alternativa, è possibile assegnare a ciascuno dei campi di testo un nome comune come "txt_" e quindi aggiungere un ID incrementale alla stringa (IE - txt_1, txt_2, txt_3, ...) quindi eseguire una iterazione programmatica sul modulo i campi che corrispondono finché non hai raggiunto un valore che rappresenta il numero totale di campi modulo disponibili. Quel valore potrebbe essere un numero intero javascript.

Per esempio ...

$("form input[type='text']").each(function() 
{ 
    // gets text value for each field in the form 
    var textFieldValue = this.value; 

    // do stuff 
}); 
0

Date un'occhiata sul codice jQuery per ottenere tutte le valutazioni Rapporto di ingresso.

$(document).ready(function() 
{ 
    $('form#d_form input[type="text"]').each(
    function() 
    { 
     alert($(this).val()); 
    }); 
}); 
Problemi correlati