加载网页后如何保存选定的选择选项下拉列表

时间:2018-01-18 10:03:59

标签: html select option

我需要一些关于这个HTML代码的帮助我试图改进。 我没有太多的HTML知识,所以最基本的解决方案会很好。

目前,代码工作正常但我希望一旦页面加载了下拉选项,就是我单击提交的选定选项的选项。与滑块相同,如果我选择2,我希望在加载后在新页面上显示2。

有没有办法用变量或如何做到这一点?



<div id="input_header">
    <div id="logo_div">
        <img id="logo" src="static/steering-wheel.svg">
        <p id="logo_name">TITLE</p>
    </div>
    <form id="driver_form" action="compare_driver" method="get">
                <p class="label">Vendor:</p>
                <select class="driver_input_box" name="driver_vendor">
                    <option value="Vendor A">AAA</option>
                    <option value="Vendor B">BBB</option>
                    <option value="Vendor C">CCC</option>
                </select>
                <p class="label"># to show: </p>
                <div id="count_slider">
                    <input id="driver_count_id" name="driver_count" type="range" min="1" max="10" step="1" value="5" oninput="driver_count_out_id.value = driver_count_id.value"/>

                    <output id="driver_count_out_id" name="driver_count_output" >5</output>
                </div>
        <input id="compare_driver" type="submit" value="Compare">

    </form>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

首先,你需要在那里加入一些JavaScript。最简单(但可能更糟)的方法是向HTML添加脚本标记。

要使用Javascript获取所选选项,您可以查看以下答案:Get selected value in dropdown list using JavaScript?。如果您向<select>提供<select id="mySelect" class="driver_inpu...,则代码将更容易编写,例如:<script> //check if there's an old selection by the user: if (sessionStorage.getItem("selectedOption")) { //to set the selected value: document.getElementById("mySelect").value = sessionStorage.getItem("selectedOption"); } //this will set the value to sessionStorage only when user clicks submit document.getElementById("driver_form").addEventListener("submit", () => { //to get the selected value: var selectedOption = document.getElementById("mySelect").value; sessionStorage.setItem("selectedOption", selectedOption); }); /* //use this only if you want to store a new value //every time the user clicks on another option document.getElementById("mySelect").addEventListener("change", () => { //to get the selected value: var selectedOption = document.getElementById("mySelect").value; sessionStorage.setItem("selectedOption", selectedOption); }); */ </script>

window.onload

如果您愿意,可以将其放入MS Access DB。这个答案应该让你走上正轨。我很累,我没有测试过,但它应该可以工作。

相关问题