简体   繁体   English

使用html中的提交按钮,基于单选按钮重定向到网页

[英]Redirect to web page based on radio button using submit button in html

My webpage takes inputs from users using a form with radio buttons. 我的网页使用带有单选按钮的form从用户那里获取输入。 Once user completes the form with all radio buttons and click submit button, it needs to redirect to web page based on the radio button selected 用户使用所有单选按钮完成表单并单击“提交”按钮后,需要根据所选的单选按钮重定向到网页

I have multiple fieldsets within the form, but i wanted to use only specific radio buttons for redirect purpose. 我在表单中有多个字段集,但我只想使用特定的单选按钮进行重定向。 so i refer the 'id' in input. 所以我在输入中引用'id'。

I tried onSubmit and action in the form, both doesn't seems helping. 我在表单中尝试了提交和操作,两者似乎都没有帮助。

<form onSubmit="javascript:redirect();" method="post"> <form onSubmit="javascript:redirect();" method="post"> <form action="javascript:redirect();"> <form onSubmit="javascript:redirect();" method="post"> <form action="javascript:redirect();">

if i use action, it simply redirects to first html page for all the radio button inputs , whereas onSubmit doesn't redirect at all 如果我使用动作,它只是重定向到所有单选按钮输入的第一个html页面,而onSubmit根本没有重定向

Also tried the multiple functions, but no luck yet 还尝试了多种功能,但还没有运气

<script type="text/javascript">
    function redirect() {
    var textValue = document.getElementById("answer").value;
    if(textValue == "Compassionate")
    {
        location.href = "../Results/results_empathy.html";
    }
    else if(textValue == "Meticulous")
    {
        location.href = "../Results/results_analytical.html";
    }
    else if(textValue == "Conscientious")
    {
        location.href = "../Results/results_diplomatic.html";
    }
    else
    {
        location.href = "../Results/results_creative.html";
    }
}
</script>

<form action="javascript:redirect();">

    <fieldset class="ntset">
    <legend>About Yourself</legend>
  <h4>Your Character in One Word</h4>
         <input type="radio" id="answer" name="habits2" value="Compassionate"><label class="fmt" for="habits2"> Compassionate</label>
     <input type="radio" id="answer" name="habits2" value="Meticulous"><label class="fmt" for="habits2"> Meticulous</label><br>
          <input type="radio" id="answer" name="habits2" value="Conscientious"><label class="fmt" for="habits2"> Conscientious </label>
     <input type="radio" id="answer" name="habits2" value="Playful"><label class="fmt" for="habits2"> Playful</label><br>

<p><input type="submit" value="Submit"  class="button" ></p>
    <p><input type="reset" value="Reset" class="button" ></p>

</form>

Also tried the following function 还尝试了以下功能

function checkAnswer(){
    var response = document.getElementById('answer').value;
    if (response == "Compassionate")
        location = '../Results/results_empathy.html';
        location.href = "../Results/results_empathy.html";
    else if (response == "Meticulous")
        location = '../Results/results_analytical.html';
        location.href = "../Results/results_analytical.html";
    else if (response == "Conscientious")
        location = '../Results/results_diplomatic.html';
        location.href = "../Results/results_diplomatic.html";    
    else if (response == "Playful")
        location = '../Results/results_creative.html';
        location.href = "../Results/results_creative.html";
    else
        location = 'wrong.html';
    return false;
}```

Expected Results are : 
If radio button selected for "Compassionate", then it has redirect to "results_empathy.html", similarly for "Meticulous" "Conscientious"

Instead of document.getElementById('answer'); 而不是document.getElementById('answer'); , use : , 采用 :

document.querySelector('input[name="habits2"]:checked').value

See if that helps you 看看这对你有帮助

When a form is submitted, it will automatically redirect to the location specified in the action attribute. 提交form ,它将自动重定向到action属性中指定的位置。 If you have a submit callback that tries to set location.href that won't work because of the nature of form submissions. 如果您有一个submit回调试图设置location.href ,由于表单提交的性质,该回调将无效。

What you need to do is either not use a submit button (and therefore the submit event) and just use a regular button and its click event or you need to cancel the native submit event and manually control the redirect. 您需要做的是不使用submit按钮(因此submit事件),只使用常规button及其click事件,或者您需要取消本机submit事件并手动控制重定向。

NOTES: 笔记:

  • With radio buttons, you need to get the value of the group, not any one button. 使用单选按钮,您需要获取组的值,而不是任何一个按钮。 Since the group all share the same name, but only one can be selected at a time, then the group will only ever have a single value. 由于该组共享相同的名称,但一次只能选择一个,因此该组将只有一个值。 It's that value you need to check. 这是你需要检查的价值。

  • You are missing your closing fieldset tag. 您缺少结束fieldset标记。

  • javascript: isn't needed. javascript:不需要。
  • type=text/javascript isn't needed. 不需要type=text/javascript
  • Don't set up your event handlers with HTML attributes. 不要使用HTML属性设置事件处理程序。 Do it separately in JavaScript. 在JavaScript中单独执行。
  • Don't use HTML elements because of the way they make content look. 不要使用HTML元素,因为它们使内容看起来很像。 You are using an h4 element in your fieldset but an h4 should only ever be used after an h3 . 您正在使用的h4元素在你的fieldset而是h4应该永远只在后使用h3 Heading tags are for creating sections, not for styling. 标题标签用于创建部分,而不是用于样式。

OPTION 1: Use regular button without submit event: 选项1:使用常规按钮而不submit事件:

 <form action="#"> <fieldset class="ntset"> <legend>About Yourself</legend> <h1>Your Character in One Word</h1> <input type="radio" id="answer" name="habits2" value="Compassionate"> <label class="fmt" for="habits2"> Compassionate</label> <input type="radio" id="answer" name="habits2" value="Meticulous"> <label class="fmt" for="habits2"> Meticulous</label><br> <input type="radio" id="answer" name="habits2" value="Conscientious"> <label class="fmt" for="habits2"> Conscientious</label> <input type="radio" id="answer" name="habits2" value="Playful"> <label class="fmt" for="habits2"> Playful</label><br> <p><input type="button" value="Submit" class="button"> <input type="reset" value="Reset" class="button"></p> </fieldset> </form> <script> // Get all the element references you'll need just once let answer = document.querySelector("[name='habits2']"); let submit = document.querySelector("input[type='button']"); // Configue event handlers in JavaScript, not with HTML attributes submit.addEventListener("click", redirect); function redirect() { if(answer.value == "Compassionate"){ location.href = "../Results/results_empathy.html"; }else if(textValue == "Meticulous"){ location.href = "../Results/results_analytical.html"; }else if(textValue == "Conscientious"){ location.href = "../Results/results_diplomatic.html"; }else { location.href = "../Results/results_creative.html"; } } </script> 

OPTION 2: Use a form with submit button and cancel the submit event: 选项2:使用带有提交按钮的表单并取消提交事件:

 <form action="#"> <fieldset class="ntset"> <legend>About Yourself</legend> <h1>Your Character in One Word</h1> <input type="radio" id="answer" name="habits2" value="Compassionate"> <label class="fmt" for="habits2"> Compassionate</label> <input type="radio" id="answer" name="habits2" value="Meticulous"> <label class="fmt" for="habits2"> Meticulous</label><br> <input type="radio" id="answer" name="habits2" value="Conscientious"> <label class="fmt" for="habits2"> Conscientious</label> <input type="radio" id="answer" name="habits2" value="Playful"> <label class="fmt" for="habits2"> Playful</label><br> <p><input type="submit" value="Submit" class="button"> <input type="reset" value="Reset" class="button"></p> </fieldset> </form> <script> // Get all the element references you'll need just once let answer = document.querySelector("[name='habits2']"); let form = document.querySelector("form"); // Configue event handlers in JavaScript, not with HTML attributes form.addEventListener("submit", redirect); function redirect(event) { // Cancel the form's native reaction to a submit event.preventDefault(); if(answer.value == "Compassionate"){ location.href = "../Results/results_empathy.html"; }else if(textValue == "Meticulous"){ location.href = "../Results/results_analytical.html"; }else if(textValue == "Conscientious"){ location.href = "../Results/results_diplomatic.html"; }else { location.href = "../Results/results_creative.html"; } } </script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM