Ciao a tutti,
vorrei un parere su questo script che crea campi dinamicamente,
devo aggiungere una select,
ho provato ad inserirne una ma quando vado a creare un nuovo gruppo la select non mantiene il value selezionato, si azzera.
Qualcuno ha un'idea di come fare?
grazie in anticipo.
il form
codice:
<form name="myForm" id="myForm" action="update.php" method="post" >
<section>
<div id="note"></div>
<div class="dati">
<p id="parah">Aggiungi un gruppo</p><br>
<div class="aggiungi">
<input type="button" onClick="javascript:addInput()" value="" class="button-aggiungi">
</div>
</div>
<div id="result"></div>
</section>
<bottone id="cbutton">
<input type="submit" name="Invia" value="Salva" id="button">
</bottone>
Il javascript
codice:
/* Campo dinamico ***************************************************/
var arrInput = new Array(0);
var arrInputValue = new Array(0);
var arrInputValue1 = new Array(0);
var arrInputValue2 = new Array(0);
var arrInputValue3 = new Array(0);
function addInput()
{
arrInput.push(arrInput.length);
arrInputValue.push("");
arrInputValue1.push("");
arrInputValue2.push("");
arrInputValue3.push("");
display();
}
function display()
{
document.getElementById('parah').innerHTML = "";
for (intI = 0; intI < arrInput.length; intI++) {
document.getElementById('note').innerHTML = "* note";
document.getElementById('parah').innerHTML += createInput(arrInput[intI], arrInputValue[intI], arrInputValue1[intI], arrInputValue2[intI], arrInputValue3[intI]);
}
}
function saveValue(intId, strValue){arrInputValue[intId] = strValue;}
function saveValue1(intId, strValue){arrInputValue1[intId] = strValue;}
function saveValue2(intId, strValue){arrInputValue2[intId] = strValue;}
function saveValue3(intId, strValue){arrInputValue3[intId] = strValue;}
function createInput(id, value, value1, value2, value3){
var str = "<table width='100%' id='tabella' border='0'><tbody><tr>";
/* campo 1 */
str += '<td>';
str += "<input type='hidden' value='"+(id+1)+"' name='numero_campi'>";
str += "<input type='text' placeholder='Ambiente' id='test " + id + "' name='Campo 1" + (id+1) + "' onChange='saveValue(" + id + ",this.value)' value='" + value + "' >";
str += '</td>';
/* campo 2 */
str += '<td>';
str += "<input type='num' onkeypress=\"onlyNumbers(event)\" placeholder='Campo 2' id='test " + id + "' name='campo2" + (id+1) + "' onChange='saveValue1(" + id + ",this.value)' value='" + value1 + "'>";
str += '</td>';
/* campo 3 */
str += '<td>';
str += "<input type='num' onkeypress=\"onlyNumbers(event)\" placeholder='Campo 3' id='test " + id + "' name='campo3" + (id+1) + "' onChange='saveValue2(" + id + ",this.value)' value='" + value2 + "'>";
str += '</td>';
/* campo 4 */
str += '<td>';
str += "<input type='num' onkeypress=\"onlyNumbers(event)\" placeholder='Campo 4' id='test " + id + "' name='campo4" + (id+1) + "' onChange='saveValue3(" + id + ",this.value)' value='" + value3 + "'>";
str += '</td>';
/* bottone elimina*/
str += '<td>';
str += "<div class='bottone'><input type='button' onClick='javascript:deleteInput()' value='' class='button-elimina'></div>";
str += '</td>';
str += "</tr></tbody></table>";
return str;
}
function deleteInput()
{
if (arrInput.length > 0) {
arrInput.pop();
arrInputValue.pop();
arrInputValue1.pop();
arrInputValue2.pop();
arrInputValue3.pop();
}
display();
}
//********************************************************************