visto che ci sono, ho recuperato delle funzioni che ho in libreria, più generali e forse più utili.
metto la pagina
codice:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Pagina senza titolo</title> <script language="javascript" type="text/javascript"> // <!CDATA[ function Button1_onclick() { //var a = elementi_lista(); var a = getElements("body", "input", "text", "lista_"); for(var i = 0; i < a.length; i++) { alert(a[i].value); } } function elementi_lista() { var a = []; var es = document.getElementsByTagName("*"); for(var i = 0; i < es.length; i++) { if(es[i].id && es[i].id.substr(0, 6) == "lista_") a.push(es[i]); } return a; } /*-------------------------------------------------------------------------- trova gli elementi che soddisfano il criterio in un vettore: source: contenitore o id contenitore. se null = body tagName: se null = "*" type: se null non ne tengo conto idSearch: se valorizzato, tutti gli elementi con id che inizia con idSearch ----------------------------------------------------------------------------*/ function getElements(source, tagName, type, idSearch) { var v = []; var result = getTypeParameter(source); if(result == 0) source = document.body; else if(result == 2) source = document.getElementById(source); else if(result == 4) source = source; else return v; tagName = (tagName == undefined)? "*" : tagName; type = (type == undefined)? "" : type; idSearch = (idSearch == undefined)? "" : idSearch; var elementi = source.getElementsByTagName(tagName); var n = elementi.length; for(var i = 0; i < n; i++) { var elemento = elementi[i]; if( (elemento.type != undefined) && ((elemento.type.toLowerCase() == type.toLowerCase()) || (type == "")) ) { if( (elemento.id != undefined) && (elemento.id.indexOf(idSearch, 0) == 0 || idSearch == "") ) { v[v.length] = elemento; } } } return v; } /*---------------------------------------------------------- Restituisce il tipo di parametro ricevuto 0 -> indefinito 1 -> funzione 2 -> id oggetto che supporta innerHTML 3 -> id oggetto che supporta value 4 -> oggetto che supporta innerHTML 5 -> oggetto che supporta value 6 -> vettore -----------------------------------------------------------*/ function getTypeParameter(v) { if(typeof(v) == "undefined") { //alert("undefined"); return 0; } else if(typeof(v) == "function") { //alert("funzione"); return 1; } else if(typeof(v) == "string") { if(document.getElementById(v) != undefined) { if(document.getElementById(v).value != undefined) { //alert(" id value"); return 3; } else if(document.getElementById(v).innerHTML != undefined) { //alert("id innerHTML"); return 2; } } else return 0; } else if(typeof(v) == "object") { if(v == null) { //alert("null"); return 0; } else { if(v.length != undefined) { //alert("vettore"); return 6; } else { if(v.value != undefined) { //alert(" oggetto value"); return 5; } else if(v.innerHTML != undefined) { //alert("oggetto innerHTML"); return 4; } } } } //indefinito o altro return 0; } // ]]> </script> </head> <body> <input id="Button1" type="button" value="button" onclick="return Button1_onclick()" /> <input type="text" id="lista_0" value="valore_0" /> <input type="text" id="lista_1" value="valore_1" /> <input type="text" id="lista_2" value="valore_2" /> <input type="text" id="lista_3" value="valore_3" /> </body> </html>

Rispondi quotando