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

    individuare tutte le select di una pagina

    Ho la necessità di individuare tutte le select (drop down) all'interno della pagina. Esiste un modo in javascript?

    grazie

  2. #2
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    guarda se può servire :master:

    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 = getElements(null, "select", "select-one");
        var s = "";
        for(var i = 0; i < a.length; i++)
        {
            var v = a[i];
            s += "tagName = " + v.tagName + ", type = " + v.type + ", id = " + v.id + "\n";
        }
        alert(s);
    }
    
    function Button2_onclick() 
    {
        var a = getElements("div1", "select", "select-one");
        var s = "";
        for(var i = 0; i < a.length; i++)
        {
            var v = a[i];
            s += "tagName = " + v.tagName + ", type = " + v.type + ", id = " + v.id + "\n";
        }
        alert(s);
    }
    
    /*--------------------------------------------------------------------------
    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
    flagSeach:  se valorizzato, 1 -> id inizia per idSearch
                                2 -> id contiene idSearch
                                3 -> id termina con idSearch
    ----------------------------------------------------------------------------*/
    function getElements(source, tagName, type, idSearch, flagSeach)
    {
    	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;
    	flagSeach = (flagSeach == undefined)? 1 : flagSeach;
    	
    	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) && ( ((flagSeach == 1 && elemento.id.indexOf(idSearch, 0) == 0) || idSearch == "") || ((flagSeach == 2 && elemento.id.indexOf(idSearch, 0) >= 0) || idSearch == "") || ((flagSeach == 3 && elemento.id.indexOf(idSearch, 0) == (elemento.id.length - idSearch.length) ) || 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>
        <select id="Select1"><option></option></select>
        <select id="Select2"><option></option></select>
        <div id="div1">
            <select id="Select3"><option></option></select>
            <select id="Select4" multiple="multiple"><option></option></select>
        </div>
        <input id="Button1" type="button" value="Tutti i DropDown" onclick="return Button1_onclick()" />
        <input id="Button2" type="button" value="Solo i DropDown di div1" onclick="return Button2_onclick()" />
    </body>
    </html>
    Pietro

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