<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SaCaWeb &#187; Javascript/JQuery/Ajax</title>
	<atom:link href="http://www.sacaweb.com/category/javascript-jquery-ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sacaweb.com</link>
	<description>Soluzioni a problemi quotidiani di programmazione Php, javascript, jQuery, CSS, Actionscript 3, Flash CS3, Photoshop, Java, Java 2EE, JavaBeans</description>
	<lastBuildDate>Wed, 11 Apr 2012 08:59:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Popolare una select con javascript, jQuery, Ajax, php</title>
		<link>http://www.sacaweb.com/javascript-jquery-ajax/popolare-una-select-con-javascript-jquery-ajax-php/</link>
		<comments>http://www.sacaweb.com/javascript-jquery-ajax/popolare-una-select-con-javascript-jquery-ajax-php/#comments</comments>
		<pubDate>Wed, 20 Apr 2011 23:43:51 +0000</pubDate>
		<dc:creator>sacaweb</dc:creator>
				<category><![CDATA[Javascript/JQuery/Ajax]]></category>
		<category><![CDATA[Php/MySQL]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[popolare select]]></category>
		<category><![CDATA[popolare select dinamicamente]]></category>

		<guid isPermaLink="false">http://www.sacaweb.com/?p=168</guid>
		<description><![CDATA[Al giorno d&#8217;oggi il web è pieno zeppo di pagine che, grazie a richieste asincrone (ajax), aggiornano porzioni di esse senza effettuare un caricamento di tutta la pagina.
Un &#34;problema&#34; ricorrente è popolare una select in base alla scelta effettuata in un&#8217;altra select. Un esempio è quello delle  [...]]]></description>
			<content:encoded><![CDATA[<p>Al giorno d&#8217;oggi il web è pieno zeppo di pagine che, grazie a richieste asincrone (ajax), aggiornano porzioni di esse senza effettuare un caricamento di tutta la pagina.</p>
<p>Un &quot;problema&quot; ricorrente è popolare una select in base alla scelta effettuata in un&#8217;altra select. Un esempio è quello delle regioni e città: si seleziona una regione dalla prima select e tramite ajax si popola la seconda select con le sole città della regione scelta. Ma ci sono migliaia di esempi e casi d&#8217;uso.</p>
<p><a href="http://www.sacaweb.com/blog/blogspot4/" target="_blank">Guarda Demo</a></p>
<p>Vediamo come sia semplice realizzare tutto con l&#8217;aiuto di jQuery.</p>
<p>Come esempio base ci basta un file html che contiene le select ed un file php al quale viene inoltrata la richiesta asincrona e che ci restituisce i dati in base ai parametri passati.</p>
<p>Nella sezione head del file html:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span> src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;http://code.jquery.com/jquery-1.5.2.min.js&quot;</span><span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span> charset<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;utf-8&quot;</span><span style="color: #339933;">&gt;</span>
    $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	   $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;select#primaselect&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">change</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		 $.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;select.php&quot;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>id<span style="color: #339933;">:</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>dati<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> options <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> dati.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				options <span style="color: #339933;">+=</span> <span style="color: #3366CC;">'&lt;option value=&quot;'</span> <span style="color: #339933;">+</span> dati<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">valore</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;'</span> <span style="color: #339933;">+</span> dati<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">etichetta</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/option&gt;'</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#secondaselect&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#secondaselect option:first'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'selected'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'selected'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Nel body del file html:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">b</span>&gt;</span>Scegli il tuo smartphone preferito:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">b</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">select</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;primaselect&quot;</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span>&gt;</span>Scegli<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;1&quot;</span>&gt;</span>HTC<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;2&quot;</span>&gt;</span>SAMSUNG<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;3&quot;</span>&gt;</span>MOTOROLA<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">select</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">select</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;secondaselect&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">select</span>&gt;</span></pre></td></tr></table></div>

<p>Contenuto del file php:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
  <span style="color: #000088;">$dati</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     <span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dati</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;valore&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;cod_001&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;etichetta&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Desire HD&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dati</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;valore&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;cod_002&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;etichetta&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Flyer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dati</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;valore&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;cod_003&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;etichetta&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Chacha&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dati</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;valore&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;cod_004&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;etichetta&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;HD2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     <span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dati</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;valore&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;cod_005&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;etichetta&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Galaxi S&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dati</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;valore&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;cod_006&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;etichetta&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Wave II&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dati</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;valore&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;cod_007&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;etichetta&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Omnia 7&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dati</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;valore&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;cod_008&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;etichetta&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Omnia HD&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     <span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dati</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;valore&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;cod_009&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;etichetta&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Atrix 4G&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dati</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;valore&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;cod_010&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;etichetta&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Droid Bionic&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dati</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;valore&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;cod_011&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;etichetta&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Cliq 2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dati</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;valore&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;cod_012&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;etichetta&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Bravo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #b1b100;">echo</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dati</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>In questo caso il file php recupera i dati da alcuni array costruiti ad hoc per l&#8217;esempio, ma nulla vieta (anzi si spera) che i dati siano recuperati da un db.</p>
<p>Per qualsiasi chiarimento, a disposizione.</p>
<p><a href="http://www.sacaweb.com/blog/blogspot4/" target="_blank">Guarda demo</a></p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.sacaweb.com%2Fjavascript-jquery-ajax%2Fpopolare-una-select-con-javascript-jquery-ajax-php%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe>]]></content:encoded>
			<wfw:commentRss>http://www.sacaweb.com/javascript-jquery-ajax/popolare-una-select-con-javascript-jquery-ajax-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Riscrivere url dinamicamente con jQuery</title>
		<link>http://www.sacaweb.com/javascript-jquery-ajax/riscrivere-url-dinamicamente-con-jquery/</link>
		<comments>http://www.sacaweb.com/javascript-jquery-ajax/riscrivere-url-dinamicamente-con-jquery/#comments</comments>
		<pubDate>Mon, 04 Apr 2011 22:32:51 +0000</pubDate>
		<dc:creator>sacaweb</dc:creator>
				<category><![CDATA[Javascript/JQuery/Ajax]]></category>
		<category><![CDATA[Php/MySQL]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[jquery history]]></category>
		<category><![CDATA[riscrivere url dinamicamente]]></category>
		<category><![CDATA[window.location.hash]]></category>

		<guid isPermaLink="false">http://www.sacaweb.com/?p=153</guid>
		<description><![CDATA[Com&#8217;è noto, Ajax permette di effettuare richieste asincrone ad un web server via Javascript, senza ricaricare la pagina e senza cambiare url nella barra degli indirizzi.
Le richieste asincrone effettuate con Ajax hanno spesso lo scopo di modificare parti della pagina html. Effettuando varie  [...]]]></description>
			<content:encoded><![CDATA[<p>Com&#8217;è noto, Ajax permette di effettuare richieste asincrone ad un web server via Javascript, senza ricaricare la pagina e senza cambiare url nella barra degli indirizzi.</p>
<p>Le richieste asincrone effettuate con Ajax hanno spesso lo scopo di modificare parti della pagina html. Effettuando varie richieste consecutive, ci si può trovare innanzi ad una pagina completamente diversa da quella iniziale, ma con uno stesso url nella barra degli indirizzi!</p>
<p>Esempio: Il sito <em>www.sitodiannunci.it</em>, alla pagina <em>annunci.php</em> permette di navigare tra gli annunci di una categoria mediante due pulsanti <em>avanti </em>e <em>indietro</em>. Il contenuto della pagina <em>annunci.php</em> cambia, mostrando i dati relativi al nuovo annuncio caricati via Ajax.</p>
<p>Dopo varie pressioni dei pulsanti <em>avanti </em>e <em>indietro</em>, l&#8217;utente decide di salvare la pagina tra i preferiti perchè interessato all&#8217;annuncio. Cosa succede? Viene salvata la stringa <em>www.sitodiannunci.it/annunci.php</em>, ma da questa non si riesce a&nbsp; risalire all&#8217;annuncio di interesse. E&#8217; chiaro che servirebbe una variabile da aggiungere alla <em>query string</em> che identifichi univocamente un annuncio, tipo <em>www.sitodiannunci.it/annunci.php?id=123</em>.</p>
<p>Se si modifica l&#8217;url della pagina ad ogni annuncio con un linguaggio lato server tipo php, si ottiene il ricaricamento della pagina che non è un effetto voluto (altrimenti perchè stiamo usando Ajax?). Stesso risultato se modifichiamo la proprietà <em>window.location.href</em>&nbsp; via javascript.&nbsp; Allora come risolvere???</p>
<p>Ci viene in aiuto una proprietà poco conosciuta&nbsp; dell&#8217;oggetto <em>location </em>javascript: <em>window.location<strong>.hash</strong>.</em></p>
<p>La stringa contenuta in <em>window.location.hash</em> sarà aggiunta in coda all&#8217;url della pagina, preceduta dal simbolo cancelletto <strong>#</strong> , senza ricaricare la pagina, ad esempio&nbsp; <em>www.sitoannunci.it/annunci.php<strong>#123 </strong></em></p>
<p>Salvando nei preferiti&nbsp; <em>www.sitoannunci.it/annunci.php<strong>#123 </strong></em><span style="font-style: italic;">&nbsp; la proprietà</span> <em>window.location.hash </em>conterrà la stringa 123 che può servire a risalire all&#8217;annuncio di interesse.</p>
<p>Abbiamo visto come risalire al contenuto di un annuncio&nbsp; impostando e poi leggendo la proprietà javascript <em>window.location.hash</em> .</p>
<p>E non è tutto. Si poteva mai parlare di javascript senza nominare jQuery??? Certo che no. Esiste infatti un plugin,<strong><em> jquery.history</em></strong>, che rende tutto semplicissimo.</p>
<p>jQuery history plugin aiuta a supportare i pulsanti avanti/indietro ed il bookmarks nelle applicazioni javascript. Permette di salvare lo stato nell&#8217;url hash e di ripristinarlo.</p>
<p><a href="http://www.sacaweb.com/blog/blogpost3/index.html" target="_blank">live demo</a>&nbsp;&nbsp;&nbsp; ( consultare il codice sorgente )</p>
<p>Ci sono 2 metodi per interagire con l&#8217;oggetto jQuery.history:</p>
<h3>jQuery.history.init(callback [, options])</h3>
<p>Inizializza il plugin e va richiamato una volta quando la pagina è completamente caricata.</p>
<p>&nbsp;</p>
<h3>jQuery.history.load(hash)</h3>
<p>Carica una stringa in <code>location.hash</code>,crea una nuova entry nella storia della navigazione e invoca la funzione di callback se necessario.&nbsp; A meno che&nbsp; l&#8217;hash passato come parametro sia diverso da quello corrente, la funzione di callback non è invocata.</p>
<p>
	<a href="http://tkyk.github.com/jquery-history-plugin/#documentation" target="_blank">Documentazione completa (in inglese)</a></p>
<p>&nbsp;</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.sacaweb.com%2Fjavascript-jquery-ajax%2Friscrivere-url-dinamicamente-con-jquery%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe>]]></content:encoded>
			<wfw:commentRss>http://www.sacaweb.com/javascript-jquery-ajax/riscrivere-url-dinamicamente-con-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery Vertical Scroller Plus Plugin</title>
		<link>http://www.sacaweb.com/javascript-jquery-ajax/jquery-vertical-scroller-plus-plugin/</link>
		<comments>http://www.sacaweb.com/javascript-jquery-ajax/jquery-vertical-scroller-plus-plugin/#comments</comments>
		<pubDate>Wed, 23 Feb 2011 23:21:49 +0000</pubDate>
		<dc:creator>sacaweb</dc:creator>
				<category><![CDATA[Javascript/JQuery/Ajax]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[scroller]]></category>
		<category><![CDATA[scrolling]]></category>
		<category><![CDATA[vertical scroller]]></category>

		<guid isPermaLink="false">http://www.sacaweb.com/?p=142</guid>
		<description><![CDATA[Ho scritto questo plugin jQuery che trasforma un div contenitore(wrapper) che contiene un div figlio(scroller) in un riquadro con contenuto scorrevole verticalmente usando due pulsanti su e giù.
Guarda la demo live
Testato e funzionante anche su dispositivi mobili touch screen iphone e Android !
Il  [...]]]></description>
			<content:encoded><![CDATA[<p>Ho scritto questo plugin jQuery che trasforma un div contenitore(wrapper) che contiene un div figlio(scroller) in un riquadro con contenuto scorrevole verticalmente usando due pulsanti su e giù.</p>
<p><a href="http://www.sacaweb.com/blog/blogpost2/vertical_scroller_plus.html" target="_blank">Guarda la demo live</a></p>
<p>Testato e funzionante anche su dispositivi mobili touch screen iphone e Android !</p>
<p>Il contenuto del div scroller può essere qualsiasi elemento html (anche solo una tabella, a differenza di altri plugin! <img src='http://www.sacaweb.com/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />  )</p>
<p>Il plugin calcola automaticamente l&#8217;altezza del div scroller.</p>
<p>Si possono utilizzare più istanze di scroller nella stessa pagina <strong>cambiando semplicemente gli id del div wrapper, del div scroller e dei pulsanti.</strong></p>
<p>Si può impostare di quanti pixel deve scorrere il contenuto e la velocità di scorrimento.</p>
<p>Facilissimo da usare:</p>
<ul>
<li>basta creare due div, uno dentro l&#8217;altro;</li>
<li>il div esterno sarà il contenitore;</li>
<li>il div interno sarà lo scroller;</li>
<li>due pulsanti per scorrere su e giù;</li>
<li>tutti gli elementi inseriti all&#8217;interno del div scroller scorreranno alla pressione dei pulsanti.</li>
</ul>
<p>&nbsp;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;wrapper&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;width:350px;height:120px;&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;scroller&quot;</span>&gt;</span>. . . <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;su_btn&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;button&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;su&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span> 
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;giu_btn&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;button&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;giù&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span></pre></td></tr></table></div>

<p>Includere i files jQuery e applicare il plugin al div contenitore:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://code.jquery.com/jquery-1.5.min.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jquery.verticalscrollplus-1.0.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">language</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;JavaScript&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
  $(document).ready(function(){
      $(&quot;#wrapper&quot;).verticalScrollPlus({step:100});
      $(&quot;#wrapper2&quot;).verticalScrollPlus({speed: 1500, step: 50, upID: &quot;#su_btn2&quot;, downID: &quot;#giu_btn2&quot;, scrollerID:&quot;#scroller2&quot; });
  });
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></td></tr></table></div>

<div style='color:white;text-shadow: 1px 1px 1px #000;font:bold 14px Arial;background-color:#b4e11e;padding:5px;border:1px solid red;border-radius: 10px; -moz-border-radius: 10px; -webkit-border-radius: 10px; '>
<p>Se reputi che il jQuery Vertical Scroller Plus Plugin ti sia stato utile, pensa ad una libera donazione come incentivo allo sviluppo futuro. Ti ringrazio anticipatamente.</p>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="NA3XVUZN2YUUY">
<input type="image" src="https://www.paypalobjects.com/it_IT/IT/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal - Il sistema di pagamento online più facile e sicuro!">
<img alt="" border="0" src="https://www.paypalobjects.com/it_IT/i/scr/pixel.gif" width="1" height="1"><br />
</form>
</div>
<p>Il plugin può essere scaricato da:</p>
<p style='text-decoration: line-through; '>http://plugins.jquery.com/content/verticalscrollerplus-10</p>
<p><a href='http://www.sacaweb.com/blog/blogpost2/jquery.verticalscrollplus-1.0.js' target='_blank'>jquery.verticalscrollplus-1.0.js</a></p>
<p>Plugin options:</p>
<ul>
<li><em><strong>speed</strong> : quanto è veloce l&#8217;animazione di scorrimento verticale;(default = 500)</em></li>
<li><em><strong>step</strong>: di quanti pixel scorre il contenuto ad ogni pressione di tasto su oppure giù;(default = 60)</em></li>
<li><em><strong>upID</strong>: id dell&#8217;elemento che fa partire l&#8217;animazione verso su;(default = #su_btn)</em></li>
<li><em><strong>downID</strong>: id dell&#8217;elemento che fa partire l&#8217;animazione verso giù;(default = #giu_btn)</em></li>
<li><em><strong>scrollerID</strong>: id dell&#8217;elemento il cui contenuto scorrerà verticalmente;(default = #scroller)</em></li>
</ul>
<p></p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.sacaweb.com%2Fjavascript-jquery-ajax%2Fjquery-vertical-scroller-plus-plugin%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe>]]></content:encoded>
			<wfw:commentRss>http://www.sacaweb.com/javascript-jquery-ajax/jquery-vertical-scroller-plus-plugin/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Etichetta di caricamento Ajax tipo Google</title>
		<link>http://www.sacaweb.com/javascript-jquery-ajax/etichetta-di-caricamento-ajax-tipo-google/</link>
		<comments>http://www.sacaweb.com/javascript-jquery-ajax/etichetta-di-caricamento-ajax-tipo-google/#comments</comments>
		<pubDate>Thu, 20 Jan 2011 21:07:55 +0000</pubDate>
		<dc:creator>sacaweb</dc:creator>
				<category><![CDATA[Javascript/JQuery/Ajax]]></category>
		<category><![CDATA[Ajax Loading Google Like]]></category>

		<guid isPermaLink="false">http://www.sacaweb.com/?p=91</guid>
		<description><![CDATA[Oggi vediamo un modo per realizzare un avviso di loading Ajax tipo Google.
Mi riferisco alla&#160; etichetta di loading che compare in alto nella pagina&#160; ogni qualvolta eseguiamo un&#8217;azione tipo eliminare un messaggio (che evidentemente effettua richieste Ajax) all&#8217;interno ad esempio di GMail.
Con  [...]]]></description>
			<content:encoded><![CDATA[<p>Oggi vediamo un modo per realizzare un avviso di loading Ajax tipo Google.</p>
<p>Mi riferisco alla&nbsp; etichetta di loading che compare in alto nella pagina&nbsp; ogni qualvolta eseguiamo un&#8217;azione tipo eliminare un messaggio (che evidentemente effettua richieste Ajax) all&#8217;interno ad esempio di GMail.</p>
<p>Con l&#8217;aiuto di JQuery diventa tutto semplicissimo. <a href="blog/blogpost1/loading_google_like.html" target="_blank">Vedi la demo live.</a></p>
<p>Di seguito la parte più importante del codice javascript:<br />
	&nbsp;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
         <span style="color: #009966; font-style: italic;">/* quando si preme il pulsante si carica il contenuto da un file di testo  */</span>
         $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#loadingbtn'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
             $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#contenuto'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'file.txt'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
         <span style="color: #006600; font-style: italic;">/* quando parte la richiesta Ajax mostra il div di loading, quando la richiesta
            viene completata nasconde il div  */</span>
         $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#loading'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ajaxSend</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
           $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeIn</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'slow'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ajaxComplete</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
           $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeOut</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'slow'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>&nbsp;</p>
<p>Di seguito il codice html della pagina:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>Clicca sul pulsante per caricare via Ajax del contenuto nel div sotto<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;button&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;loadingbtn&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;loadingbtn&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Carica contenuto&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;contenuto&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;contenuto&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;loading&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;loading&quot;</span>&gt;</span>Loading...<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<p>Importante la regola css che imposta lo stato iniziale dell&#8217;avviso di loading su invisibile, in quanto è su questa che va ad agire javascript al verificarsi degli eventi ajaxSend e ajaxComplete.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#loading</span><span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">fixed</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">green</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #993333;">white</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">80px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span><span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.sacaweb.com%2Fjavascript-jquery-ajax%2Fetichetta-di-caricamento-ajax-tipo-google%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe>]]></content:encoded>
			<wfw:commentRss>http://www.sacaweb.com/javascript-jquery-ajax/etichetta-di-caricamento-ajax-tipo-google/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Selezionare radiobox da jQuery</title>
		<link>http://www.sacaweb.com/javascript-jquery-ajax/selezionare-radiobox-da-jquery/</link>
		<comments>http://www.sacaweb.com/javascript-jquery-ajax/selezionare-radiobox-da-jquery/#comments</comments>
		<pubDate>Wed, 29 Dec 2010 00:18:28 +0000</pubDate>
		<dc:creator>sacaweb</dc:creator>
				<category><![CDATA[Javascript/JQuery/Ajax]]></category>

		<guid isPermaLink="false">http://www.sacaweb.com/?p=66</guid>
		<description><![CDATA[Quando selezioniamo un componente radiobox col mouse succedono almeno due cose:

Il pallino del radiobox viene annerito ad indicare che &#232; selezionato;
Viene lanciato un evento javascript di tipo onchange, che volendo pu&#242; essere intercettato.

Supponiamo di avere un gruppo di radiobox che  [...]]]></description>
			<content:encoded><![CDATA[<p>Quando selezioniamo un componente radiobox col mouse succedono almeno due cose:</p>
<ol>
<li>Il pallino del radiobox viene annerito ad indicare che &egrave; selezionato;</li>
<li>Viene lanciato un evento javascript di tipo onchange, che volendo pu&ograve; essere intercettato.</li>
</ol>
<p>Supponiamo di avere un gruppo di radiobox che rappresentano colori:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>input <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;colori&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;radio&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;red&quot;</span> <span style="color: #339933;">/&gt;</span> Rosso 
<span style="color: #339933;">&lt;</span>input <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;colori&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;radio&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;green&quot;</span> <span style="color: #339933;">/&gt;</span> Verde 
<span style="color: #339933;">&lt;</span>input <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;colori&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;radio&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;blue&quot;</span> <span style="color: #339933;">/&gt;</span> Blu
&nbsp;
<span style="color: #339933;">&lt;</span>span id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;risposta&quot;</span><span style="color: #339933;">&gt;&lt;/</span>span<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Grazie alla potenza di jQuery &egrave; semplice intercettare l&#39;evento onchange quando si seleziona un radiobox:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;input[name=colori]:radio&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">change</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#risposta&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#risposta&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;color&quot;</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Quando si seleziona un radiobox, il corrispondente colore viene scritto in un elemento span con id=&quot;risposta&quot;. Da notare anche come sia semplice modificare lo stile di un elemento.</p>
<p>Ma come si potrebbe simulare il click del mouse da jQuery?</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> colore <span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;red&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>colore <span style="color: #339933;">!=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;input[value=&quot;</span><span style="color: #339933;">+</span>colore<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;]:radio&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'checked'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Attraverso il metodo attr(), risulta semplice modificare l&#39;attributo checked del radiobox.</p>
<p>Sembrerebbe bastare, ma la modifica dell&#39;attributo checked attraverso il metodo attr() ottiene come risultato la sola selezione del radiobox, senza lanciare alcun evento onchange.</p>
<p>Dando il valore &#8220;red&#8221; alla variabile colore, si ottiene la selezione del radiobox corrispondente, ma non viene scritto nulla nell&#39;elemento con id=&#8221;risposta&#8221;. Questo perch&egrave; attraverso la sola modifica dell&#39;attributo col metodo attr() non viene lanciato l&#39;evento onchange.</p>
<p>Si pu&ograve; per&ograve; lanciare l&#39;evento onchange manualmente attraverso il codice</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&nbsp;
 $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;input[value=&quot;</span><span style="color: #339933;">+</span>colore<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;]:radio&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">change</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Il codice finale &egrave;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&lt;input name=&quot;colori&quot; type=&quot;radio&quot; value=&quot;red&quot; /&gt;Rosso
&lt;input name=&quot;colori&quot; type=&quot;radio&quot; value=&quot;green&quot; /&gt;Verde
&lt;input name=&quot;colori&quot; type=&quot;radio&quot; value=&quot;blue&quot; /&gt;Blu
&nbsp;
&lt;span id=&quot;risposta&quot;&gt;&lt;/span&gt;
&nbsp;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #003366; font-weight: bold;">var</span> colore <span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;red&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>colore <span style="color: #339933;">!=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;input[value=&quot;</span><span style="color: #339933;">+</span>colore<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;]:radio&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'checked'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;input[name=colori]:radio&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">change</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
         $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#risposta&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#risposta&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;color&quot;</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;input[value=&quot;</span><span style="color: #339933;">+</span>colore<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;]:radio&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">change</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p><a class="fancybox-iframe" href="http://www.sacaweb.com/blog/prova_radiobox.html">Guarda il risultato</a></p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.sacaweb.com%2Fjavascript-jquery-ajax%2Fselezionare-radiobox-da-jquery%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe>]]></content:encoded>
			<wfw:commentRss>http://www.sacaweb.com/javascript-jquery-ajax/selezionare-radiobox-da-jquery/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ciao mondo!!</title>
		<link>http://www.sacaweb.com/riflessioni-varie/ciao-mondo/</link>
		<comments>http://www.sacaweb.com/riflessioni-varie/ciao-mondo/#comments</comments>
		<pubDate>Wed, 10 Nov 2010 21:07:11 +0000</pubDate>
		<dc:creator>sacaweb</dc:creator>
				<category><![CDATA[Css]]></category>
		<category><![CDATA[Flash/Actionscript]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Javascript/JQuery/Ajax]]></category>
		<category><![CDATA[Photoshop]]></category>
		<category><![CDATA[Php/MySQL]]></category>
		<category><![CDATA[Riflessioni varie]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[photoshop]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.sacaweb.com/blog/?p=1</guid>
		<description><![CDATA[Benvenuto nel mio Blog!
Cercherò di aggiornarlo quotidianamente con soluzioni a problemini di programmazione Php, Javascript, Css, Actionscript, Java, Java EE, che quotidianamente qualsiasi Webmaster o Web Developer si trova ad affrontare, nella speranza di far risparmiare del tempo&#8230;
]]></description>
			<content:encoded><![CDATA[<p>Benvenuto nel mio Blog!</p>
<p>Cercherò di aggiornarlo quotidianamente con soluzioni a problemini di programmazione Php, Javascript, Css, Actionscript, Java, Java EE, che quotidianamente qualsiasi Webmaster o Web Developer si trova ad affrontare, nella speranza di far risparmiare del tempo&#8230;</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.sacaweb.com%2Friflessioni-varie%2Fciao-mondo%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe>]]></content:encoded>
			<wfw:commentRss>http://www.sacaweb.com/riflessioni-varie/ciao-mondo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

