简体   繁体   中英

How do I use javascript to get the result from radio button and display it at the last page?

I have 3 html pages: 1st 1 has a radio button question, the second page will have a second question, the 3rd page will simply show/display the result of selection. (ie. The user selects a) Apple )

The html part is easy, but I don't know how to write the javascript part.

Please help.

Try the below:

<script>
    function showQuestion()
    {
        document.getElementById("question2").style.display = 'inline';
    }
    function showResults()
    {
        document.getElementById("results").style.display = 'inline';
    }
</script>

<div id="question1">
    <select onchange="showQuestion()">
      <option value="0">Choose something</option>
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
    </select>
</div>

<div id="question2" style="display: none;">
    <select onchange="showResults()">
      <option value="0">Choose something</option>
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
    </select>
</div>

<div id="results" style="display: none;">

</div>

Note that I did not use multiple pages, as then you'd need a way to save the user data, ie what he chose, and load it on another page. Thus you'd require a server side technology or some client side tricks to remember the choice.

Also, jQuery is a fantastic way of writing javascript that's faster to write and easier to get things done. My code above is pure javascript.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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