Hola a todos. Os envio mi código
Gracias
Un saludo.
Respuestas:
a) En un if siempre hay que interpretar un boleano o un valor numérico. False equivale a 0 y true equivale a cualquier otro valor numérico. Para el presente ejercicio checked equivale a true, con independencia de que lo indique, y por lo tanto a un valor numérico distinto de cero. Unchecked equivaldría a false y de valor numérico 0.
b)c)
<!DOCTYPE html>
<html>
<head>
<title>Ejemplo JavaScript - aprenderaprogramar.com</title>
<meta charset="utf-8">
<style type="text/css">
body {background-color:white; font-family: sans-serif;}
label {color: maroon; display:inline-block; padding:5px;}
</style>
<script type="text/javascript">
function informarItemsElegidos(elemento) {
var elementosObtenidos = document.getElementsByName(elemento);
var msg = 'Animales que ha elegido que le gustan incluye: ';
var elegidos = 0;
if (elementosObtenidos[0].checked == true){msg = msg + elementosObtenidos[0].value; elegidos=elegidos+1;}
if (elementosObtenidos[1].checked == true && elegidos>=1){msg=msg+', ';}
if (elementosObtenidos[1].checked == true){msg = msg + elementosObtenidos[1].value; elegidos=elegidos+1;}
if (elementosObtenidos[2].checked == true && elegidos>=1){msg=msg+', ';}
if (elementosObtenidos[2].checked == true){msg = msg + elementosObtenidos[2].value; elegidos=elegidos+1;}
if (elementosObtenidos[3].checked == true && elegidos>=1){msg=msg+', ';}
if (elementosObtenidos[3].checked == true){msg = msg + elementosObtenidos[3].value; elegidos=elegidos+1;}
if (elegidos == 0 ) {msg = '¡No ha elegido ningún animal!';}
alert (msg+'.');
alert ('El número total de animales disponibles era '+elementosObtenidos.length+' y usted ha elegido '+elegidos);
}
</script>
</head>
<body>
<div id="cabecera">
<h1>Portal web aprenderaprogramar.com</h1>
<h2>Didáctica y divulgación de la programación</h2>
</div>
<!-- Formulario de contacto -->
<div style="width:450px;">
<form name ="formularioContacto" class="formularioTipo1" method="get" action="http://aprenderaprogramar.com" onsubmit="informarItemsElegidos('animal')">
<p>Si quieres contactar con nosotros envíanos este formulario relleno:</p>
<label for="nombre"><span>Nombre:</span> <input id="nombre" type="text" name="nombre" /></label>
<label for="apellidos"><span>Apellidos:</span> <input id="apellidos" type="text" name="apellidos" /></label>
<p>Elige los animales que te gusten:</p>
<input type="checkbox" name="animal" id="leon" value="leon" />
<label for="leon">León & nbsp;& nbsp;& nbsp; </label>
<input type="checkbox" name="animal" id="tigre" value="tigre" />
<label for="tigre">Tigre & nbsp;& nbsp;& nbsp; </label>
<input type="checkbox" name="animal" id="guepardo" value="guepardo" />
<label for="guepardo">Guepardo & nbsp;& nbsp;& nbsp; </label>
<input type="checkbox" name="animal" id="jaguar" value="jaguar" />
<label for="jaguar">Jaguar </label>
<label for="email"><span>Correo electrónico:</span> <input id="email" type="text" name="email" /></label>
<label>
<input type="submit" value="Enviar" />
<input type="reset" value="Cancelar" />
</label>
</form>
</div>
</body>
</html>