O. Formulario con radio y checbox

Salida

Ábrelo en otra pestaña.

Revísalo en gilpgedit.

1<!DOCTYPE html>
2<html lang="es">
3
4<head>
5
6 <meta charset="UTF-8">
7 <title>Radio y Checkbox</title>
8 <meta name="viewport"
9 content="width=device-width">
10
11</head>
12
13<body>
14
15 <form id="forma"
16 action="javascript:procesa()">
17
18 <h1>Radio y Checkbox</h1>
19
20 <fieldset>
21 <legend>Pasatiempos</legend>
22 <p>
23 <label>
24 <input type="checkbox"
25 name="pasatiempos[]"
26 value="fut">
27 Futbol
28 </label>
29 </p>
30 <p>
31 <label>
32 <input type="checkbox"
33 name="pasatiempos[]"
34 value="chess" checked>
35 Ajedrez
36 </label>
37 </p>
38 <p>
39 <label>
40 <input type="checkbox"
41 name="pasatiempos[]"
42 value="basket" checked>
43 Basketbol
44 </label>
45 </p>
46 </fieldset>
47
48 <fieldset>
49 <legend>Madrugador</legend>
50 <p>
51 <label>
52 <input type="radio"
53 name="madrugador" value="si"
54 checked>
55 Si
56 </label>
57 <label>
58 <input type="radio"
59 name="madrugador"
60 value="no">
61 No
62 </label>
63 </p>
64 </fieldset>
65
66 <p>
67 <button type="submit">
68 Procesar
69 </button>
70 </p>
71
72 <script>
73 function procesa() {
74 alert("Madrugador: "
75 + forma["madrugador"].value
76 )
77 const pasatiempos =
78 Array.from(
79 forma.querySelectorAll(
80 "[name='pasatiempos[]']:checked"
81 )
82 ).map(input => input.value)
83 alert("Pasatiempos: "
84 + pasatiempos.join(", "))
85 }
86 </script>
87
88 </form>
89
90</body>
91
92</html>
skip_previous skip_next