简体   繁体   中英

How to change the selected value of the dropdown based on the value

I have a requirement where the user will fill out and submit a form. If there are any errors found on the server side, the same form will be displayed to the user, but now the previously selected value of the drop down is not selected by default again. I want the default selected value of the drop down to be based the value the user selected before.

在服务器上创建下拉列表的代码应检查在打印选项时,如果选项与用户选择的选项匹配,则标记为“已选择”

You're going to some how have to maintain state between page refreshes.

For example, you could store the value in a cookie while the user is filling out the form, and when the page refreshes (with errors) you can check for the value and deal with it accordingly.

which server side language you are using? this is not looking the JavaScript problem, because after submitting the page get refreshed and you are validating on server side, so you can use POST Global vars to return the value Or you can ask me with more information

To ensure that the default selected value is always the previously selected one, you could catch the selected value on the server-side script that receives the form. Then, save this value to a session variable and compare it later with this (inside the loop that creates the dropdown) :

if (isset($_SESSION["previousValue"])) {
    if ($value == $_SESSION["previousValue"]) {
        echo '<option value="$value" selected="selected">$value</option>';
    } else {
        echo '<option value="$value">$value</option>';
    }
}

Hope this helps you

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