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

117
Views
Necesito redirigir en función de la respuesta a un envío de formulario html

por ejemplo

 <form onsubmit="submitForm(event)"> <p> Do you like cats or dogs </p> <input type="radio" id="cat" value="Cats"> <label for="cat">Cats</label> <input type="radio" id="dog" value="Dogs"> <label for="dog">Dogs</label><br> <button>Submit</button> </form> <script> function submitForm(event) { if(document.getElementById('cat').checked) { //(send the user to the cat page) }else if(document.getElementById('dog').checked) { //(send the user to the dog page) } </script>

¿Cómo enviaría a los usuarios a diferentes páginas en la misma carpeta raíz que mi sitio web en función de sus respuestas a mis preguntas?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Dale una oportunidad a window.location.href

 <form onsubmit="submitForm(event)"> <p> Do you like cats or dogs </p> <input type="radio" id="cat" value="Cats"> <label for="cat">Cats</label> <input type="radio" id="dog" value="Dogs"> <label for="dog">Dogs</label><br> <button>Submit</button> </form> <script> function submitForm(event) { if(document.getElementById('cat').checked) { //(send the user to the cat page) window.location.href = 'yoursite.com/cat' }else if(document.getElementById('dog').checked) { //(send the user to the dog page) window.location.href = 'yoursite.com/dog' } </script>
about 4 years ago · Santiago Trujillo Report

0

Editar : formato

Primero, hay algunos errores en el código que deben corregirse.

A las entradas de radio les falta el atributo de name . Sin un valor para el conjunto de atributos de name , es huérfano.

Cada una de las entradas de radio a continuación se puede verificar al mismo tiempo y daría como resultado un comportamiento inesperado.

Además, falta una llave al final de la función de JavaScript 'submitForm'

 <form onsubmit="submitForm(event)"> <p> Do you like cats or dogs </p> <!-- Missing 'name' attribute' --> <input type="radio" id="cat" value="Cats"> <label for="cat">Cats</label> <!-- Missing 'name' attribute' --> <input type="radio" id="dog" value="Dogs"> <label for="dog">Dogs</label><br> <button>Submit</button> </form> <script> function submitForm(event) { if (document.getElementById('cat').checked) { //(send the user to the cat page) } else if (document.getElementById('dog').checked) { //(send the user to the dog page) } // <-Missing closing curly brace for 'submitForm' </script>

Para solucionar estos problemas, aquí está el código HTML que publicó con las partes faltantes completadas. Tenga en cuenta que ahora solo se puede seleccionar una de las opciones.

 <form onsubmit="submitForm(event)"> <p>Do you like cats or dogs</p> <!-- Now has a name attribute set to 'PetChoiceRadioGroup' --> <input type="radio" id="cat" value="Cats" name="PetChoiceRadioGroup" /> <label for="cat">Cats</label> <!-- Now has a name attribute set to 'PetChoiceRadioGroup' --> <input type="radio" id="dog" value="Dogs" name="PetChoiceRadioGroup" /> <label for="dog">Dogs</label><br> <button type="submit">Submit</button> </form> <script> function submitForm(event) { if (document.getElementById('cat').checked) { //(send the user to the cat page) } else if (document.getElementById('dog').checked) { //(send the user to the dog page) } } // <-Curly brace no longer missing </script>

Después de solucionar estos problemas, la respuesta de kawnah es una buena solución usando location.href para cambiar las páginas según la opción seleccionada.

about 4 years ago · Santiago Trujillo 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!