Ho modificato lo script che mi hai segnalato secondo le mie esigenze e l'ho trasformato in questo:
codice:
var numfield=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 oP=document.createElement("P");
var oFieldA=document.createElement("INPUT");
var oFieldB=document.createElement("INPUT");
var oFieldC=document.createElement("INPUT");
var oButt=document.createElement("INPUT");
var oText1=document.createTextNode("");
var oText2=document.createTextNode("nome: ");
var oText3=document.createTextNode("prezzo: ");
// setta attributi
oFieldA.setAttribute("type","text");
oFieldA.setAttribute("name","g_variable_n_"+numfield);
oFieldA.setAttribute("class","inputbox");
oFieldA.setAttribute("value","0");
oFieldA.setAttribute("style","width:30px;margin-right:10px;");
oFieldB.setAttribute("type","text");
oFieldB.setAttribute("name","g_variable_var_"+numfield);
oFieldB.setAttribute("class","inputbox");
oFieldB.setAttribute("style","width:300px;margin-right:10px;");
oFieldC.setAttribute("type","text");
oFieldC.setAttribute("name","g_variable_price_"+numfield);
oFieldC.setAttribute("class","inputbox");
oFieldC.setAttribute("style","width:60px;margin-right:10px;");
oButt.setAttribute("type","button");
oButt.setAttribute("value","rimuovi");
// setta gestore evento
if(oButt.attachEvent) oButt.attachEvent('onclick',function(e){rimuovi(e);})
else if(oButt.addEventListener) oButt.addEventListener('click',function(e){rimuovi(e);},false)
// appendi al relativo padre
oTd1.appendChild(oP);
oP.appendChild(oText1);
oP.appendChild(oFieldA);
oP.appendChild(oText2);
oP.appendChild(oFieldB);
oP.appendChild(oText3);
oP.appendChild(oFieldC);
oP.appendChild(oButt);
oTr.appendChild(oTd1);
oTr.appendChild(oTd2);
document.getElementById('gvariable').getElementsByTagName('TBODY')[0].appendChild(oTr);
// incrementa variabile globale
numfield++
}
}
function rimuovi(e){
if(document.removeChild && document.getElementById && document.getElementsByTagName) {
if(!e) e=window.event;
var srg=(e.target)?e.target:e.srcElement;
// risali al tr del td che contiene l' elemento che ha scatenato l' evento
while(srg.tagName!="TR"){srg=(srg.parentNode)?srg.parentNode:srg.parentElement}
// riferimento al tbody
var tb=document.getElementById('gvariable').getElementsByTagName('TBODY')[0];
// rimuovi
tb.removeChild(srg);
}
}
Funziona bene... l'unica cosa che non ho capito è come scrivere in un hidden field il contatore dei box aggiunti che corrisponde nel mio caso alla variabile numfield