Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    onclick usando HTML DOM

    Ho un array bidimensionale Items[i][j] e ciclando su questo array sto creando una tabella con i valori dell'array. Vorrei che quando clicko su una riga i della tabella mi dicesse il valore array Items[i][0]
    La riga di codice che non funziona è:

    riga.onclick = function() {alert(Items[i][0])};

    cmq sentitevi liberi di suggerire qualsiasi tipo di miglioramento sul codice.

    Grazie.

    codice:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    
    <script language="JavaScript" type="text/javascript">
    <!--
    	var Items = new Array();
    	var Item = new Array(); 
    	Item[0] = '00005'; 
    	Item[1] = '00005';
    	Item[2] = '';
    	Item[3] = '';
    	Item[4] = 'COMUNE DI PROVA ';
    	//assegno al array che popola la Select questo ente
    	Items[Items.length] = Item;
    
    	var Item = new Array(); 
    	Item[0] = '06954A1'; 
    	Item[1] = '06954';
    	Item[2] = 'A';
    	Item[3] = '1';
    	Item[4] = 'COMUNE DI BARI  UFFICIO TRIBUTI UFF...';
    	//assegno al array che popola la Select questo ente
    	Items[Items.length] = Item;
    
    
    
    
    function GeneraTabella( divId) {
    	nrighe = Items.length;
    	var divElem;
    	var tableId = "pseudoselect";
    	var tableClass = "pseudoselect";
    	var tableFontSize = "8pt";
    	var tableFontColor = "#4a569e";
    	var trStyle = "cursor:pointer";
    	var ncolonne=4;
    	var bordo = '1px solid #4a569e';
    	var marginecelle = 0;
    	var spaziocelle=0;
    	try{
    		if (document.createElement && document.getElementsByTagName) { // controllo accesso alle funzionalità DOM
    
    			divElem = document.getElementById(divId);
    			
    			tabella=document.createElement('TABLE');
    			corpo=document.getElementsByTagName('BODY').item(0);
    			corpotab=document.createElement('TBODY');
    			for (i=0; i<Items.length; i++) {
    				riga=document.createElement('TR');
    				for (j=0; j<ncolonne; j++) {
    					cella=document.createElement('TD');
    					testo=document.createTextNode(Items[i][j+1]);
    					cella.appendChild(testo);
    					riga.appendChild(cella);
    				}
    				riga.style.cursor='pointer';
    				riga.onmouseover = function() {this.style.color = '#ffffff'; this.style.background = '#316ac5'};
    				riga.onmouseout = function() {this.style.color = '#4a569e'; this.style.background = '#ffffff'};
    				riga.onclick = function() {alert(Items[i][0])};
    				corpotab.appendChild(riga);
    				
    			}
    			tabella.style.border = bordo;
    			tabella.cellSpacing = spaziocelle;
    			tabella.cellPadding = marginecelle;
    			tabella.style.fontSize = tableFontSize;
    			tabella.id = tableId;
    			
    			tabella.style.color = tableFontColor;
    			tabella.appendChild(corpotab);
    			
    			divElem.appendChild(tabella);
    			divElem.style.display = "block";
    		}else 
    			alert('Spiacente, il tuo browser non supporta lo standard DOM.');
    	}catch(e){alert(e)}			
    	}	//
    	
    
    
    //-->
    </script>
    
    </head>
    <body onload="GeneraTabella('listcompanies');">
    	<div id="listcompanies"  class="lcompanies"> </div>
    </body>
    </html>
    Some people make things happen, some see them happen, others ask what happened...

  2. #2
    Ho capito come devo fare

    Grazie lo stesso.......
    Some people make things happen, some see them happen, others ask what happened...

  3. #3
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    problema abbastanza frequente
    quando clicchi sei fuori dal ciclo che genera la tabella,
    le variabili, se non ulteriormente valorizzate, contengono l' ultimo valore che e' stato loro attribuito nel ciclo

    per mantenere un riferimento all' oggetto corretto ti consiglio il sistema utilizzato e spiegato in questa discussione

    http://forum.html.it/forum/showthrea...hreadid=808632

    ciao

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 © 2024 vBulletin Solutions, Inc. All rights reserved.