-1

ラジオ ボタンで選択した値を表示しようとしています。フォームは次のとおりです。

<form onsubmit="return false;">

<fieldset>
<h3>Textos</h3>
<label>Nombre:</label>
<input type="text" name="nombre"/>
    <label>Apellido:</label>
    <input type="text" name="apellido" />
    <label>Contraseña:</label>
    <input type="password" name="contrasena" />
    <div class="limpia"></div>
</fieldset>
<fieldset>
<h3>Gustos musicales:</h3>
    <label>POP <input type="checkbox" name="pop" value="POP" class="gmusicales"    
    /></label>
    <label>ROCK <input type="checkbox" name="rock" value="ROCK" class="gmusicales"  
    /></label>
    <label>HIP-HOP <input type="checkbox" name="hiphop" value="HIP-HOP"  
    class="gmusicales" 
    /></label>
    <label>METAL <input type="checkbox" name="metal" value="METAL" class="gmusicales" 
    /></label>
    <div class="limpia"></div>
</fieldset>
<fieldset>
<h3>Sistema operativo</h3>
    <label>Windows <input type="radio" name="SO" value="Windows" class="soperativo"/> 
    </label>
    <label>Linux <input type="radio" name="SO" value="Linux" class="soperativo"/> 
    </label>
    <label>Mac <input type="radio" name="SO" value="Mac" class="soperativo"/></label>
    <div class="limpia"></div>
</fieldset>
<fieldset>
    <input type="reset" value="Borrar"/>
    <input type="submit" onclick=muestra() value="Probar" />
    <div class="limpia"></div>
</fieldset>

</form>

問題は私のjavascriptコードにあります:

var nom;
var ape;
var con;
var gustos = document.getElementsByClassName("gmusicales");
var sistema = document.getElementById("soperativo");
var resultado;
var i;
var h2 = document.createElement("h2");

function muestra()
{
nom = document.forms[0].elements.nombre.value;
ape = document.forms[0].apellido.value;
con = document.forms[0].contrasena.value;
resultado = nom + " " + ape + " " + con;

for(i=0; i<=gustos.length; i++)
  {
    if (gustos[i].checked)
      {
        resultado = resultado + " " + gustos[i].value;
        alert(resultado);
      }
  } 

  for(i=0; i<=sistema.length; i++)
  {
    if (sistema[i].checked)
      {
        resultado = resultado + " " + sistema[i].value;
        alert(resultado);
      }
  }

document.body.appendChild(h2); 
document.h2.appenChild(resultado);  

}

私が欲しいのは、sistemaoperativoフィールドセットの値を表示することです。選択できるのは 1 つだけです。それを選択すると、何も表示されません。もう 1 つのチェックボックスを選択すると、値が表示されます。h2 html タグを作成し、値を出力したいと考えています。

どうすればいいですか?

ありがとう。

4

2 に答える 2