Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

119
Vistas
JavaScript para obtener valor de la selección html y vincular al botón

 var gch; document.addEventListener('DOMcontentLoaded', Function()) { gch = document.getElementById('choose'); if (localStorage['choose']) { gch = input; } input.onchange = function() { gch = this.value; } } function Visit() { if (gch == 1) { window.location = "index.html"; } else if (gch == 2) { window.location = "input.html"; } }
 <select class="form-select" id='choose' aria-label="Default select example"> <option selected>Choose an Action:</option> <option value="1">ADD Patient</option> <option value="2">Delete Patient</option> </select> <button onclick="Visit()">go</button>

Quería crear un menú desplegable con "Agregar nombre de paciente" y "Eliminar nombre de paciente" como opciones y un "¡Ir!" botón para ir a la página correspondiente desde el inicio. Era realmente nuevo en js, todavía usaba la lógica de java para escribir js, no puedo entender qué está mal

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Tenías un montón de errores de sintaxis y errores tipográficos.

DOMcontentLoaded debe tener una C mayúscula.

Function debe tener una f minúscula.

El paréntesis de cierre en la llamada addEventListener debe estar después del cuerpo de la función.

Asignó gch al elemento <select> , cuando debería ser input .

No estoy seguro de lo que está tratando de hacer con localStorage , y localStorage no se puede usar en Stack Snippets debido a su sandboxing, así que lo dejé fuera. Sospecho que lo que quieres es gch = localStorage.getItem("choose");

 var gch; document.addEventListener('DOMContentLoaded', function() { var input = document.getElementById('choose'); input.onchange = function() { gch = this.value; } }) function Visit() { if (gch == 1) { window.location = "index.html"; } else if (gch == 2) { window.location = "input.html"; } }
 <select class="form-select" id='choose' aria-label="Default select example"> <option selected>Choose an Action:</option> <option value="1">ADD Patient</option> <option value="2">Delete Patient</option> </select> <button onclick="Visit()">go</button>

about 4 years ago · Juan Pablo Isaza Denunciar

0

También puedes hacer eso...

 document .addEventListener( 'DOMContentLoaded', () => { const inChoose = document.getElementById('choose') , gch = { add : 'index.html' , del : 'input.html' } document .getElementById('bt-go') .onclick = () => { if (gch[inChoose.value]) window.location = gch[inChoose.value] } })
 <select class="form-select" id='choose' aria-label="Default select example"> <option selected>Choose an Action:</option> <option value="add">ADD Patient</option> <option value="del">Delete Patient</option> </select> <button id="bt-go">go</button>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Hubo algún error y error de sintaxis en document.addEventListener y la función dentro de document.addEventListener.

Prueba esto:

 let gch; document.addEventListener('DOMContentLoaded', function() { const input = document.getElementById('choose'); input.onchange = function () { gch = this.value; } }) function Visit() { if (gch == 1) { console.log("Uncomment below line to redirect to index.html"); // window.location = "index.html"; } else if (gch == 2) { console.log("Uncomment below line to redirect to input.html"); // window.location = "input.html"; } }
 <select class="form-select" id='choose' aria-label="Default select example"> <option selected>Choose an Action:</option> <option value="1">ADD Patient</option> <option value="2">Delete Patient</option> </select> <button onclick="Visit()">go</button>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda