Salve a tutti ho una pagina in cui la griglia del tris viene generata tramite un cliclo for con "document.write" e quello che vorrei fare è sostituire il document.write con l'operatore "+=" ma visualizzo l'errore "document.forms[0].elements[i].value is not defined". Allego i codici della pagina:
codice:for (i = 1; i < 10; i++) { if (i % 3 == 1) { document.write('<tr>'); } document.write('<td><Input Type="Button" Name="' + i + '" Value=" " Class="block" onClick="change(this.form,this)"></td>'); if (i % 3 == 0) { document.write('</tr>'); } }Grazie per la disponibiltà, ho provato a modificare il ciclo for con il metodo dell'operatore "+=" ma visualizzo l'errore che ho descritto sopra.codice:timerID = xnum = ynum = znum = auto = 0; who = 'Giocatore 1'; function init() { cell = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0); add = new Array(); empty = add; turnstep = exch = 1; wins = times = 0; document.forms[0].player1.value = xnum; document.forms[0].player2.value = ynum; document.forms[0].draw.value = znum; if (who != 'Start' || true) { for (i = 0; i < cell.length; i++) { cell[i] = 0; if (i > 0) { document.forms[0].elements[i].value = ''; }; } } document.forms[0].turn.value = who; } function win() { if (cell[1] + cell[2] + cell[3] == 3 || cell[4] + cell[5] + cell[6] == 3 || cell[7] + cell[8] + cell[9] == 3 || cell[1] + cell[4] + cell[7] == 3 || cell[2] + cell[5] + cell[8] == 3 || cell[3] + cell[6] + cell[9] == 3 || cell[1] + cell[5] + cell[9] == 3 || cell[3] + cell[5] + cell[7] == 3) { if (confirm("Giocatore 1\: hai vinto!\nVuoi giocare di nuovo?")) { wins = 1; xnum++; who = 'Giocatore 1'; timerID = init(); } else { xnum++; who = 'Start'; timerID = init(); }; } else if (cell[1] + cell[2] + cell[3] == 30 || cell[4] + cell[5] + cell[6] == 30 || cell[7] + cell[8] + cell[9] == 30 || cell[1] + cell[4] + cell[7] == 30 || cell[2] + cell[5] + cell[8] == 30 || cell[3] + cell[6] + cell[9] == 30 || cell[1] + cell[5] + cell[9] == 30 || cell[3] + cell[5] + cell[7] == 30) { if (confirm("Giocatore 2\: hai vinto!\nVuoi giocare di nuovo?")) { ynum++; who = 'Giocatore 1'; timerID = init(); } else { ynum++; who = 'Start'; timerID = init(); }; } else if (times == 9) { if (confirm("Pareggio!\n\nVuoi giocare di nuovo?")) { znum++; who = 'Giocatore 1'; timerID = init(); } else { znum++; who = 'Start'; timerID = init(); }; } } function exchange(turnstep) { if (exch == 1) { if (turnstep == 0) { turnstep = 1; who = "Giocatore " + 1 } else { if (auto == 0) { turnstep = 0; who = "Giocatore " + 2 } else { turnstep = 0; who = "Computer"; } } } else { turnstep = turnstep; } exch = 1; return (turnstep); } function changeing(click) { place = cell[click]; clicked = click; if (place == 0) { if (turnstep == 0) { xo = "O"; place = 10; } else { xo = "X"; place = 1; } cell[click] = place; times++; } else { if (place == 10) { xo = "O"; } if (place == 1) { xo = "X"; } exch = 0 } return (xo); } function change(form, element) { changeing(element.name); element.value = xo; turnstep = exchange(turnstep); form.turn.value = who; win(); } init();

Rispondi quotando
