简体   繁体   English

如何使用下一步按钮 go 到下一页,然后通过提交按钮显示结果?

[英]How to go to the next page using next button and later display result by submit button?

So, from this code, I tried to make the next button go to the next page that will display the male section if users clicked on the answer is 'male' for gender (vice versa).因此,从这段代码中,我尝试将下一个按钮 go 设置为下一页,如果用户单击性别的答案是“男性”(反之亦然),该页面将显示男性部分。 After user complete the survey, then only all the values from main page and male page will output after clicking the submit button.用户完成调查后,点击提交按钮后,只有主页和男性页面中的所有值将 output 。 But I can't find the correct way to do it.但我找不到正确的方法来做到这一点。

I have corrected some mistakes here which I accidentally overlook.我在这里纠正了一些我不小心忽略的错误。 But, I still have problem on the next button not going to the appropriate page section.但是,我仍然对下一个按钮没有转到相应的页面部分有问题。 It only go to male section eventho i put the condition to go to female section as well.它只有 go 到男性部分,即使我也将条件到 go 到女性部分。

//Let's focus on the male part... I will do the female part later, thanks! //让我们关注男性部分...我稍后会做女性部分,谢谢!

This is the Main page (mainsection.html)这是主页 (mainsection.html)

<form  id="form1">
    <p>Gender</p>
    <label><input type="radio" name="gender" id="male" value="Male">Male</label>
    <label><input type="radio" name="gender" id="female" value="Female">Female</label>
    <button id= "next" value="Next">Next</button>
</form>

    <script type="text/javascript" src="script.js"></script>

This is the Male section page (malesection.html)这是男性部分页面 (malesection.html)

<form  id="form1">
    <p>Height</p>
    <label><input type="radio" name="height" id="short" value="Short"><165cm</label>
    <label><input type="radio" name="height" id="average" value="Average">165cm-175cm</label>
    <label><input type="radio" name="height" id="tall" value="Tall">>175cm</label>

    <p>Wrist Size</p>
    <label><input type="radio" name="wrist" id="small" value="small"><15cm</label>
    <label><input type="radio" name="wrist" id="medium" value="medium">15cm-15.6cm</label>
    <label><input type="radio" name="wrist" id="large" value="large">>15.6cm</label>

    <button type="submit" value="Submit">Submit</button>
    <p id="ans"></p>
    <p id="ans2"></p>

</form>

    <script type="text/javascript" src="script.js"></script>

This is the JavaSript code (script.js)这是 JavaSript 代码 (script.js)

<script>
    document.getElementById("form1").onsubmit=function() {

    var x = document.getElementById("ans");
    var y = document.getElementById("ans2");
    var m = document.getElementById("male");
    var f = document.getElementById("female");
    var t = document.getElementById("teen");
    var a = document.getElementById("adult");
    var s = document.getElementById("senior");
    var mHS = document.getElementById("short");
    var mWS = document.getElementById("small");

   //Next button condition
   document.getElementById("next").onclick = function()
   {
     if(m.checked)
        location.href = "malesection.html";

     else if(f.checked)
        location.href = "femalesection.html";
   }

     if(t.checked){x.innerHTML = "You are a Teen."}
       else if(a.checked){x.innerHTML = "You are an Adult."}
           else if(s.checked){x.innerHTML = "You are a Senior."}

     if(m.checked){
       if(mHS.checked){
          if(mWS.checked)
             {y.innerHTML = "You are a male with short height and small body 
                             frame. Thus, you have an Ectomorph body type. Choose 
                             clothes that neatly skim the body."}
       }
    }
    return false;
}
</script>
    <script type="text/javascript">
        document.getElementById("next").onclick = next() {
        location.href = "malesection.html";
    </script>
    };

That is wrong.那是错的。 You close the "function" but have the close tag for the script block within the function.您关闭“函数”,但在 function 中有脚本块的关闭标记。 Also, that's not how you declare a function.此外,这不是您声明 function 的方式。

To define an event handle, you do the following:要定义事件句柄,请执行以下操作:

document.getElementById('next').onclick = function(e)
{
  location.href = "malesection.html";
};

But I expect that will fail as you haven't set the id of the button correctly, also you have set the onclick handler in the attributes of the button, so the handler is set twice.但我希望这会失败,因为您没有正确设置按钮的 id,而且您已经在按钮的属性中设置了 onclick 处理程序,因此处理程序设置了两次。 Except that there is no function called "next".除了没有 function 称为“下一个”。

So, change your HTML to something like:因此,将您的 HTML 更改为:

<button id="next">Next</button>

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

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