Stessa funzione tra 2 o più form

Danelius

Nuovo Utente
20 Apr 2011
1
0
0
Ciao ragazzi! Ho un piccolo problema con uno script per avviare una funzione tra form diversi, però non capisco dove sbaglio

PHP:
<html>
<head>
<script type="text/javascript" language="javascript">
function prova(mioForm)
{
	alert(document.forms[mioForm].animale.value);
}
</script>
</head>

<body>

<form name="form1">
<select name="animale">
  <option value="gatto">Gatto</option>
  <option value="cane">Cane</option>
  </select>
<input type="button" value="bottone1" onclick="prova(form1);">
</form>

<form name="form2">
<select name="animale">
  <option value="serpente">Serpente</option>
  <option value="lucertola">Lucertola</option>
  </select>
<input type="button" value="bottone2" onclick="prova(form2);">
</form>


</body>
</html>
:book:
 
HTML:
<html>
<head></head>
<body>

<form name="form1">
<select name="animale" id="animale">
  <option value="gatto">Gatto</option>
  <option value="cane">Cane</option>
  </select>
<input type="button" value="bottone1" onclick="alert(document.getElementById('animale').value);" />
</form>

<form name="form2">
<select name="animale" id="animale2">
  <option value="serpente">Serpente</option>
  <option value="lucertola">Lucertola</option>
  </select>
<input type="button" value="bottone2" onclick="alert(document.getElementById('animale2').value);" />
</form>

</body>
</html>

Oppure:

HTML:
<html>
<head></head>
<body>

<form name="form1">
<select name="animale" id="animale">
  <option value="gatto">Gatto</option>
  <option value="cane">Cane</option>
  </select>
<input type="button" value="bottone1" onclick="alert(this.form.animale.value);" />
</form>

<form name="form2" id="form2">
<select name="animale" id="animale2">
  <option value="serpente">Serpente</option>
  <option value="lucertola">Lucertola</option>
  </select>
<input type="button" value="bottone2" onclick="alert(this.form.animale2.value);" />
</form>

</body>
</html>
Meglio usare il DOM usando gli id... ricorca che gli id in una pagina devono essere univoci
 
Ultima modifica:

Discussioni simili