javascript - How to maintain a persistent state of the drop down? - Stack Overflow

I am making a simple web app that has drop down list in the first page in a form tags.Now when I chang

I am making a simple web app that has drop down list in the first page in a form tags. Now when I change the value in the dropdown the form is submitted. On the onchange I have made call to create a query string custom JS function of the selected Index. Now I want the drop down list to maintain its state; keep the last selected value selected after submission.
How can I do this?

I am making a simple web app that has drop down list in the first page in a form tags. Now when I change the value in the dropdown the form is submitted. On the onchange I have made call to create a query string custom JS function of the selected Index. Now I want the drop down list to maintain its state; keep the last selected value selected after submission.
How can I do this?

Share Improve this question edited Apr 19, 2021 at 18:11 munity wiki
4 revs, 2 users 73%
SocialCircus 2
  • 1 Shouldn't have made it munity wiki – gn22 Commented Oct 16, 2009 at 7:06
  • Are you using a server-side language such as ASP or PHP or this just pure HTML? I'm assuming the form submits back to itself (same page)? – WesleyJohnson Commented Oct 18, 2009 at 14:29
Add a ment  | 

4 Answers 4

Reset to default 3

You add the selected attribute with any (e.g. blank) value to the you want to keep selected.

The getQueryValue function is a bit unoptimized and would probably be better acplished with a regular expression match to get the value, but I just typed it out like this so you could see what it was doing.

<html>
<head>
<script type="text/javascript">
function setSelections()
{
    document.myForm.mySelect.value = getQueryValue("mySelect");
};

function getQueryValue(key)
{
    var queryString = window.location.search.substring(1);
    var queryParams = queryString.split("&");
    for(var i = 0; i < queryParams.length; i++)
    {
        if(queryParams[i].indexOf("=") > 0)
        {
            var keyValue = queryParams[i].split("=");
            if(keyValue[0] == key)
            {
                return keyValue[1];
            }
        }
    }

    return null;
}
</script>
</head>
<body onload="setSelections();">
    <form name="myForm" action="" method="get">
        <select id="mySelect" name="mySelect" onchange="document.myForm.submit();">
            <option value="Opt1">Option 1</option>
            <option value="Opt2">Option 2</option>
            <option value="Opt3">Option 3</option>
            <option value="Opt4">Option 4</option>
        </select>
    </form>
</body>
</html>

Depends on how your page is dealt with after submission, if it goes to a PHP/Perl or whatever language to rebuild the page, then put the drop down options into a script and loop through them.

Something like this is pretty mon place

for($i = 0; $i < 10; $i++){
echo "<option value='{$i}' ";
($i == $_POST['dropdownname'])? echo "selected='selected'":"";
echo ">{$i}</option>";
}

You could pass a hidden form variable to the next posted page. Using a simple Javascript function that loops through the options of the selection drop-down list, if any of the options match the hidden value, then set its selected sttribute.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744208357a4563214.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信