Visualizzazione dei risultati da 1 a 4 su 4

Discussione: Textbox dinamiche

  1. #1
    Utente di HTML.it
    Registrato dal
    Apr 2013
    Messaggi
    19

    Textbox dinamiche

    Salve,
    di seguito posto un codice molto utile scritto da uno dei vostri moderatori in una vecchia discussione. Questo codice permette di aggiungere e rimuovere dinamicamente delle textbox.
    A me servirebbe sapere come potrei fare a inserire in questo codice una textbox che mostri il valore della variabile num e che si aggiorni automaticamente (ad ogni click del bottone accoda o rimuovi).

    codice:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    
    <html>
    <head>
    <title>aggiungi / rimuovi campi input</title>
    <script language="JavaScript" type="text/javascript">
    <!--
    var num=1;
    function accoda(){
    	if(document.createElement && document.getElementById && document.getElementsByTagName) {
    		// crea elementi
    		var oTr=document.createElement("TR");
    		var oTd1=document.createElement("TD");
    		var oTd2=document.createElement("TD");
    		var oField=document.createElement("INPUT");
    		var oText=document.createTextNode("riga "+num);
    
    		// setta attributi
      		oField.setAttribute("type","text");
    		oField.setAttribute("name","testo"+num);
    
    		// appendi al relativo padre
    		oTd1.appendChild(oField);
    		oTd2.appendChild(oText);
    		oTr.appendChild(oTd1);
    		oTr.appendChild(oTd2);
    		document.getElementById('tabella').getElementsByTagName('TBODY')[0].appendChild(oTr);
    
    		// incrementa variabile globale
    		num++
    	}
    }
    
    function rimuovi(){
    	if(document.removeChild && document.getElementById && document.getElementsByTagName) {
    		// se non e' la prima riga
    		if(num>1){
    			// riferimento al tbody
    			var tb=document.getElementById('tabella').getElementsByTagName('TBODY')[0];
    			// riferimento all' ultimo TR
    			var lastTr=tb.getElementsByTagName('TR')[(tb.getElementsByTagName('TR').length)-1]
    			// rimuovi
    			tb.removeChild(lastTr);
    			// decrementa variabile globale
    			num--
    		}
    	}
    }
    //-->
    </script>
    </head>
    <body>
    <form name="modulo">
    <input type="button" value="accoda" onclick="accoda()"> <input type="button" value="rimuovi" onclick="rimuovi()"> 
    <table border="1" id="tabella">
    <tbody>
    <tr>
    <td><input type="text" name="testo0" /></td><td>riga 0</td>
    </tr>
    </tbody>
    </table>
    </form>
    </body>
    </html>

  2. #2
    Utente di HTML.it L'avatar di carlomarx
    Registrato dal
    Oct 2009
    Messaggi
    1,669
    Se ho capito bene quello che chiedi...

    codice:
    [...]
    
    		// setta attributi
      		oField.setAttribute("type", "text");
    		oField.setAttribute("name", "testo"+num);
    		oField.setAttribute("value", num);
    
    [...]
    Tutto ciò che fai con jQuery puoi farlo meglio e con la metà del codice in puro JavaScript.

  3. #3
    Utente di HTML.it
    Registrato dal
    Apr 2013
    Messaggi
    19
    Originariamente inviato da carlomarx
    Se ho capito bene quello che chiedi...

    codice:
    [...]
    
    		// setta attributi
      		oField.setAttribute("type", "text");
    		oField.setAttribute("name", "testo"+num);
    		oField.setAttribute("value", num);
    
    [...]
    No...mi serve creare proprio un'altra text box, non assegnare il valore num alle textbox che vengono create

  4. #4
    Moderatore di Annunci siti web, Offro lavoro/collaborazione, Cerco lavoro L'avatar di cavicchiandrea
    Registrato dal
    Aug 2001
    Messaggi
    26,133
    codice:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    
    <html>
    <head>
    <title>aggiungi / rimuovi campi input</title>
    <script language="JavaScript" type="text/javascript">
    <!--
    var num=1;
    function accoda(){
    	if(document.createElement && document.getElementById && document.getElementsByTagName) {
    		// crea elementi
    		var oTr=document.createElement("TR");
    		var oTd1=document.createElement("TD");
    		var oTd2=document.createElement("TD");
    		var oField=document.createElement("INPUT");
    		var oText=document.createTextNode("riga "+num);
    
    		// setta attributi
      		oField.setAttribute("type","text");
    		oField.setAttribute("name","testo"+num);
    
    		// appendi al relativo padre
    		oTd1.appendChild(oField);
    		oTd2.appendChild(oText);
    		oTr.appendChild(oTd1);
    		oTr.appendChild(oTd2);
    		document.getElementById('tabella').getElementsByTagName('TBODY')[0].appendChild(oTr);
    
    		// incrementa variabile globale
    		num++
    	}
    document.getElementById("contatore").value=num
    }
    
    function rimuovi(){
    	if(document.removeChild && document.getElementById && document.getElementsByTagName) {
    		// se non e' la prima riga
    		if(num>1){
    			// riferimento al tbody
    			var tb=document.getElementById('tabella').getElementsByTagName('TBODY')[0];
    			// riferimento all' ultimo TR
    			var lastTr=tb.getElementsByTagName('TR')[(tb.getElementsByTagName('TR').length)-1]
    			// rimuovi
    			tb.removeChild(lastTr);
    			// decrementa variabile globale
    			num--
    		}
    	}
    document.getElementById("contatore").value=num
    }
    //-->
    </script>
    </head>
    <body>
    <form name="modulo">
    <input type="button" value="accoda" onclick="accoda()"> <input type="button" value="rimuovi" onclick="rimuovi()"> 
    <table border="1" id="tabella">
    <tbody>
    <tr>
    <td><input type="text" name="testo0" /></td><td>riga 0</td>
    </tr>
    <tr>
    <td><input type="text" name="contatore" id="contatore" /></td><td>conta</td>
    </tr>
    </tbody>
    </table>
    </form>
    </body>
    </html>
    Cavicchi Andrea
    Problemi con javascript, jquery, ajax clicca qui

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.