2017-10-20 42 views
1

HTML - 这是一个简单的HTML表单,我有JS来验证,如果请一些能够帮助我......检查表单使用JavaScript(由CBX选择正确的选项)

<form> 
<label class="ancho">Nombre</label> 
<input id="name" class="bloque" type="text" placeholder="Nicolas"> 
<label class="ancho">Apellido</label> 
<input id="surename" class="bloque" type="text" placeholder="Oyarzun"> 
<label class="ancho">Edad</label> 
<input id="age" class="bloque" type="number" placeholder="27" min="1" max="110"> 
<label class="ancho">Carrera</label> 
    <select id="cbxCarrera" class="bloque"> 
     <option id="op0">-- Seleccione una Carrera --</option> 
     <option id="op1">Programacion</option> 
    </select> 
    <button class="bloque boton" onclick="formulario()">Enviar</button 
</form> 

的JavaScript

<script> 
    var nombre = document.getElementById("name"); 
    var apellido = document.getElementById("surename"); 
    var edad = document.getElementById("age"); 
    var carrera = document.getElementById("cbxCarrera"); 
    var opcion0 = document.getElementById("op0"); 
    var opcion1 = document.getElementById("op1"); 

    function formulario() { 
     while (nombre != null || apellido != null || edad > 0) { 
      if (document.getElementById("cbxCarrera").value != "0") { 
       // just checking if displays 
       alert("nombre: " + nombre\ "apellido: " + apellido\ "edad: " + edad\ "carrera: " 
        document.getElementById(id).optSelected); 
      } 
     } else { 
      alert("Porfavor ingrese valores validos en los campos anteriores."); 
     } 
    } 
</script> 

我该如何验证在组合框中选择的选项? 谢谢!

回答

0

您可以使用的selectedIndex这样

carrera.options[carrera.selectedIndex]; 

获得所选择的选项,然后用它做什么,请你,

REFERENCE

相关问题