PDA

View Full Version : [javascript] condizione radio-button


glider59
28-09-2007, 10:30
Salve a tutti
vista l'inesperienza sono qui a chiedere un aiuto. Il seguente form va compilato in tutte le parti ma se si scegli il radio-button "SI" bisogna compilare anche il campo seguente altrimenti si salta a quello successivo.
sono 2 giorni che :mc: e :muro: , ma con la sceta SI-NO non riesco a farlo funzionare :help: :help: qualcuno puō aiutarmi??
GRAZIE ANTICIPATAMENTE.
<html>
<head>
<title>Untitled</title>
<script language="javascript">
function verifica() {
var attivita=document.form1.attivita.value;
var dove=document.form1.dove.value;
var scelta=0;
var sport=document.form1.sport.value;
var SI=0;
//Effettua il controllo sul campo ATTIVITā
if ((attivita == "") || (attivita == "undefined")) {
alert("Il campo ATTIVITA' č obbligatorio");
document.form1.attivita.focus();
return false;
}
//Effettua il controllo sul campo SCELTA'
for(var i=0;i<document.form1.scelta.length;i++) {
if(document.form1scelta.checked==true){
scelta=1
};
};
if (scelta==0){
alert("Scegli SI o NO");
return false;
};
//Effettua il controllo sul campo DOVE
for(var a=0;a<document.form1.scelta.length;a++){
if (document.form1.scelta[a].checked==true) {
var SI=(document.form1.scelta[a].value);
};
};
if (SI==1) {
if ((dove == "") || (dove == "undefined")) {
alert("Hai scelto SI, il campo DOVE č obbligatorio");
document.form1.dove.focus();
};
};
else if ((sport == "") || (sport == "undefined")) {
alert("Il campo SPORT č obbligatorio");
document.form1.sport.focus();
return false;
}
else {
document.form1.action = "dati.php";
document.form1.submit();
}
}
</script>
</head>
<body>
<form action="" method="post" name="form1">

<table summary="">
<tr><td>Attivitā <input type="text" name="attivita" value="" /> </td>
</tr>
<tr><td>SI <input type="radio" name="scelta" value=1 />
NO <input type="radio" name="scelta" value=0 /> </td>
</tr>
<tr><td>Se si dove? <input type="text" name="dove" value="" /> </td>
</tr>
<tr><td>sport <input type="text" name="sport" value="" /></td>
</tr>
<tr><td colspan="4">&nbsp;</td></tr>
<tr><td align="center"><input type="button" value="Invia" onclick="verifica()" />
<input type="reset" name="canc" value="cancella" /></td>
</tr>
</table>
</form>
</body>
</html>

isAlreadyInUse
28-09-2007, 10:56
Tič
<html>
<head>
<title>Untitled</title>

<script type="text/javascript">

function validateForm(){

var returnValue=true;
var msg="";
frm=document.getElementById('form1');

if(document.getElementById('attivita').value==""){
returnValue=false;
msg="Inserire l'attivitā";
}
if(document.getElementById("scelta").checked){
if(document.getElementById('dove').value=="" || document.getElementById('sport').value==""){
returnValue=false;
msg="Selezionare gli altri campi";
}
}
if(returnValue){
frm.action = "dati.php";
frm.submit();
}else{
alert(msg);
}
}
</script>
</head>
<body>
<form action="" method="post" name="form1" id="form1">
<table summary="">
<tr>
<td>Attivitā <input type="text" id="attivita" name="attivita" value="" /></td>
</tr>
<tr>
<td>
<input type="radio" id="scelta" name="scelta" value="0" />SI
<input type="radio" id="scelta" name="scelta" value="1" />NO
</td>
</tr>
<tr>
<td>Se si dove? <input type="text" id="dove" name="dove" value="" /> </td>
</tr>
<tr>
<td>Sport <input type="text" id="dove" name="sport" value="" /></td>
</tr>
<tr>
<td colspan="4">&nbsp;</td></tr>
<tr>
<td align="center">
<input type="button" value="Invia" onclick="validateForm();" />
<input type="reset" name="canc" value="cancella" /></td>
</tr>
</table>
</form>
</body>
</html>

glider59
29-09-2007, 09:01
Grazie per la pronta rispota :D , ora cerco di adattarla.