Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
Cómo redirigir según la entrada del usuario javascript

No puedo encontrar una forma de trabajo para redirigir a los usuarios en mi sitio web en función de lo que escriben en un campo de texto. Intenté usar este código a continuación pero no tuve éxito. ¿Puede alguien por favor guiarme?

 function valForm() { var firstVal = document.getElementById("nextId"); if (firstVal.length == 0) { error += "Desk Number is required\n"; } if (firstVal == 430) { window.location = "https://awebsite"; } if (error) { alert(error); } }
 <input type="text" placeholder="License Plate" class="form-control" id="name-form9-z" /> <div class="ticketId"> <input type="text" placeholder="Ticket ID" class="form-control" id="nextId"> </div> <button class="btn btn-primary display-4" type="button" onclick="valform()">next</button>

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

En primer lugar, tiene un error tipográfico en la etiqueta de su botón. Cambie valform() a valForm() . Y también hay algunos errores en su función.

  • Defina el error de la variable antes de que se use en la función valForm .
  • var firstVal = document.getElementById("nextId") debe ser var firstVal = document.getElementById("nextId").value ;
  • window.location= "https://awebsite" debe ser window.location.href = "https://awebsite";

A continuación se muestra el código completo.

 function valForm() { var firstVal = document.getElementById("nextId").value; var error = '' if (firstVal.length == 0) { error += "Desk Number is required\n"; } if (firstVal == 430) { window.location.href = "https://awebsite"; } if (error) { alert(error); } }
about 4 years ago · Juan Pablo Isaza Report

0

  1. JS distingue entre mayúsculas y minúsculas, por lo que valForm ambos lugares
  2. Las entradas tienen valor, por lo que var firstVal = document.getElementById("nextId").value;
  3. No definiste error
  4. Puede convertir a número usando el plus no articulado u otros métodos

Sin embargo, está bien usar window.location sin .href, pero se recomienda usar location.href o window.location.href solo para acostumbrarse a .href si alguna vez desea LEER la cadena

Le sugiero que use un evento de envío en su lugar y use addEventListener para no tener controladores de eventos en línea. Envolví los elementos en una etiqueta de formulario para usar el evento de envío del formulario

 document.getElementById("myForm").addEventListener("submit", function(e) { e.preventDefault(); // stop submission let error = ""; var firstVal = document.getElementById("nextId").value; console.log(firstVal) if (firstVal.length == 0) { error += "Desk Number is required\n"; } if (+firstVal === 430) { window.location = "https://awebsite"; } if (error) { alert(error); } })
 <form id="myForm"> <input type="text" placeholder="License Plate" class="form-control" id="name-form9-z" /> <div class="ticketId"> <input type="text" placeholder="Ticket ID" class="form-control" id="nextId"> </div> <button class="btn btn-primary display-4">next</button> </form>

about 4 years ago · Juan Pablo Isaza Report

0

A partir de ahora, solo tiene una referencia DOM de la entrada.

var firstVal = document.getElementById("nextId"); .

Todavía tiene que obtener el valor.

var firstVal = document.getElementById("nextId").value; .

También tenga en cuenta que le falta una notación de mayúsculas y minúsculas en su llamada de función. JavaScript distingue entre mayúsculas y minúsculas.

<button class="btn btn-primary display-4" type="button" onclick="valform()">next</button> .

debiera ser:

<button class="btn btn-primary display-4" type="button" onclick="valForm()">next</button>

Código completo:

 function valForm() { var firstVal = document.getElementById("nextId").value; if (firstVal.length == 0) { error += "Desk Number is required\n"; } if (firstVal == 430) { window.location.href = "https://awebsite"; } if (error) { alert(error); } }
 <input type="text" placeholder="License Plate" class="form-control" id="name-form9-z" /> <div class="ticketId"> <input type="text" placeholder="Ticket ID" class="form-control" id="nextId"> </div> <button class="btn btn-primary display-4" type="button" onclick="valForm()">next</button>

Tendré que asumir que el error está definido en algún lugar fuera del código que proporcionó; de lo contrario, arrojará un error de error sin definir.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!