Visualizzazione dei risultati da 1 a 4 su 4

Discussione: Campi form dinamico

  1. #1

    Campi form dinamico

    Ciao a tutti su internet ho trovato un bello scrippettino che faceva al caso mio, solo che vorrei apportargli una modifica:

    Codice originale:
    codice:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"></meta>
    <title></title>
    <script language="Javascript" type="text/javascript">
    <!--
    //Add more fields dynamically.
    function addField(area,field,limit) {
    	if(!document.getElementById) return; //Prevent older browsers from getting any further.
    	var field_area = document.getElementById(area);
    	var all_inputs = field_area.getElementsByTagName("input"); //Get all the input fields in the given area.
    	//Find the count of the last element of the list. It will be in the format '<field><number>'. If the
    	//		field given in the argument is 'friend_' the last id will be 'friend_4'.
    	var last_item = all_inputs.length - 1;
    	var last = all_inputs[last_item].id;
    	var count = Number(last.split("_")[1]) + 1;
    
    	//If the maximum number of elements have been reached, exit the function.
    	//		If the given limit is lower than 0, infinite number of fields can be created.
    	if(count > limit && limit > 0) return;
    
    	if(document.createElement) { //W3C Dom method.
    		var li = document.createElement("li");
    		var input = document.createElement("input"); 
                    input.id = field+count;
    		input.name = 'campo[]';//field+count;
    		input.type = "text"; //Type of field - can be any valid input type like text,file,checkbox etc.
                    
    		li.appendChild(input);
                    field_area.appendChild(li);
    	} else { //Older Method*/
    		field_area.innerHTML += "[*]<input name='campo[]' id='"+(field+count)+"' type='text' />";
    	}
    }
    //-->
    </script>
    </head>
        <body>
    <form method="POST" action="" name="formsondaggio">
    
    1. <input name="campo[]" id="fields_1" type="text" /> <a onclick="this.parentNode.parentNode.removeChild(this.parentNode);"> Remove Field </a>
    2. <input name="campo[]" id="fields_1" type="text" /> <a onclick="this.parentNode.parentNode.removeChild(this.parentNode);"> Remove Field </a>
    3. <input name="campo[]" id="fields_1" type="text" /> <a onclick="this.parentNode.parentNode.removeChild(this.parentNode);"> Remove Field </a>
    <input type="submit" name="invio" /><input type="reset" name="reset" /> <input value="Add Field" onclick="addField('pollfields','fields_',0);" type="button" /> </form> </body> </html>
    Vorrei modificarlo in modo da ottenere anche affianco ai nuovi campi aggiunti, la scritta remove field, perchè lo vedo più usabile in questo modo.
    Da quel poco che so di javascript la modifica andrebbe dopo

    codice:
    if(document.createElement)
    {
        //qui inserire modifica
    }else{...}
    ma cosa ci si deve mettere??

    ho provato una modifica rozza giusto per vedere se funzionava, ovvero lasciando solo questa stringa

    codice:
    field_area.innerHTML += "[*]<input name='campo[]' id='"+(field+count)+"' type='text' /><a onclick='this.parentNode.parentNode.removeChild(this.parentNode);'>Remove Field</a>";
    senza l'else e tutto funziona, ma volevo capire a cosa serve l'if con i suoi elementi dentro e se c'è un modo per non cancellare quella parte di codice.
    spero di essermi fatto capire
    e soprattutto spero possiate aiutarmi a chiarirmi un po' le idee.
    Grazie Ciao

  2. #2
    Moderatore di Annunci siti web, Offro lavoro/collaborazione, Cerco lavoro L'avatar di cavicchiandrea
    Registrato dal
    Aug 2001
    Messaggi
    26,133
    Non ho verificato lo script, sicuramente non va bene avere gli id tutti uguali id="fields_1" per le modifiche leggendo questa discussione dovresti risolvere
    Cavicchi Andrea
    Problemi con javascript, jquery, ajax clicca qui

  3. #3
    Ciao
    Beh a parte gli id uguale, ma ad ogni modo lo script in quel modo funziona.
    L'unica cosa che vorrei fare è avere un link o una bottone per ogni riga da eliminare...

    Nell'if ho aggiunto le seguenti righe

    var BRimuovi = document.createElement("input");

    BRimuovi.type = "button";
    BRimuovi.name = "Remove";
    BRimuovi.value = "Remove";

    e mi funzia ovvero crea un bottone per ogni nuova riga aggiunta, solo che non so come associargli un evento...onclick con il codice per rimuoverlo.

  4. #4
    HOLEEEEE
    Ho trovato la soluzione, ora funziona come volevo io...vi posto il codice giusto se qualcuno lo volesse...

    codice:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"></meta>
    <title></title>
    <style type="text/css">
        .opzione{margin-right: 10px;}
    </style>
    </head>
    <body>
    <form method="POST" action="" name="formsondaggio">
    
    1. <input name="campo[]" id="fields_1" type="text" class="opzione" /><a onclick="this.parentNode.parentNode.removeChild(this.parentNode);">Remove</a>
    2. <input name="campo[]" id="fields_2" type="text" class="opzione" /><a onclick="this.parentNode.parentNode.removeChild(this.parentNode);">Remove</a>
    3. <input name="campo[]" id="fields_3" type="text" class="opzione" /><a onclick="this.parentNode.parentNode.removeChild(this.parentNode);">Remove</a>
    <input type="submit" name="invio" /><input type="reset" name="reset" /> <input value="Add Field" onclick="addField('pollfields','fields_',0);" type="button" /> </form> <script language="Javascript" type="text/javascript"> <!-- //Add more fields dynamically. function addField(area,field,limit) { if(!document.getElementById) return; //Prevent older browsers from getting any further. var field_area = document.getElementById(area); var all_inputs = field_area.getElementsByTagName("input"); //Get all the input fields in the given area. //Find the count of the last element of the list. It will be in the format '<field><number>'. If the // field given in the argument is 'friend_' the last id will be 'friend_4'. var last_item = all_inputs.length - 1; var last = all_inputs[last_item].id; var count = Number(last.split("_")[1]) + 1; alert("allinputs: "+all_inputs[last_item].id+"\nlast_item: "+last_item+"\nlast: "+last+"\ncount: "+count+"\nfield+count: "+(field+count)); //If the maximum number of elements have been reached, exit the function. // If the given limit is lower than 0, infinite number of fields can be created. if(count > limit && limit > 0) return; if(document.createElement) { //W3C Dom method. var li = document.createElement("li"); var input = document.createElement("input"); var BRimuovi = document.createElement("a"); BRimuovi.innerHTML = "Remove"; BRimuovi.setAttribute("onclick", "this.parentNode.parentNode.removeChild(this.parentNode);"); input.setAttribute("id", field+count); input.setAttribute("name", 'campo[]'); input.setAttribute("type", "text"); //Type of field - can be any valid input type like text,file,checkbox etc. input.setAttribute("class", "opzione"); li.appendChild(input); li.appendChild(BRimuovi); field_area.appendChild(li); //alert("allinputs: "+all_inputs+"\nlast_item: "+last_item+"\nlast: "+last+"\ncount: "+count+"\nfield+count: "+(field+count)); } else { //Older Method field_area.innerHTML += "[*]<input name='campo[]' id='"+(field+count)+"' type='text' /><a onclick='this.parentNode.parentNode.removeChild(this.parentNode);'>Remove</a>"; } } //--> </script> </body> </html>

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.