admin管理员组

文章数量:1289957

I have a select list on a page like this:

<select name='elements'>
    <option value='water'>Water</option>
    <option value='fire'>Fire</option>
    <option value='air'>Air</option>
</select>

EDIT: When a user makes a selection, the current page is refreshed, but how do I keep the user's selection visible in the list after the page refresh? In other words, I don't want the list to rollback to its default selected value.

Help please

I have a select list on a page like this:

<select name='elements'>
    <option value='water'>Water</option>
    <option value='fire'>Fire</option>
    <option value='air'>Air</option>
</select>

EDIT: When a user makes a selection, the current page is refreshed, but how do I keep the user's selection visible in the list after the page refresh? In other words, I don't want the list to rollback to its default selected value.

Help please

Share Improve this question edited Mar 26, 2014 at 13:57 Chuck Le Butt 48.8k62 gold badges209 silver badges297 bronze badges asked Jun 7, 2012 at 22:39 user765368user765368 20.4k27 gold badges101 silver badges171 bronze badges 2
  • 1 Is there any reason you want a full page refresh? why not simply show/hide/populate from an async call...? – balexandre Commented Jun 7, 2012 at 22:41
  • I wanna translate the content of the whole page into another language, so yes I want a full page refresh – user765368 Commented Jun 7, 2012 at 22:45
Add a ment  | 

3 Answers 3

Reset to default 4

Here is a Javascript-only solution.

 <select name='elements' id='elements' onChange='window.location="yoururl.html?value=" + this.value;'>
     <option value='water'>Water</option>
     <option value='fire'>Fire</option>
     <option value='air'>Air</option>
 </select>

Then you use this function to see if the value parameter is set and get it and you can use jQuery to set it as selected.

$("#elements option[value='" + result_of_gup + "']").attr("selected","selected") ;
 <select name='elements' onChange='window.location="yoururl.php?value=" + this.value;'>
     <option value='water'>Water</option>
     <option value='fire'>Fire</option>
     <option value='air'>Air</option>
 </select>

Doing it without jQuery:

Create window.location.getParameter() function (see here from Anatoly Mironov) then:

<select name='elements' id='elements' onChange='window.location="yoururl.html?elements=" + this.selectedIndex;'>
   <option value='water'>Water</option>
   <option value='fire'>Fire</option>
   <option value='air'>Air</option>
</select>

<script type="text/javascript">
  els = document.getElementById('elements');
  selIndex = window.location.getParameter('elements') || 0;
  els[selIndex].selected = true;​
</script>

For ease of reference, I reproduce Anatoly's excellent .getParameter function below:

window.location.getParameter = function(key) {
    function parseParams() {
        var params = {},
            e,
            a = /\+/g,  // Regex for replacing addition symbol with a space
            r = /([^&=]+)=?([^&]*)/g,
            d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
            q = window.location.search.substring(1);

        while (e = r.exec(q))
            params[d(e[1])] = d(e[2]);

        return params;
    }

    if (!this.queryStringParams)
        this.queryStringParams = parseParams(); 

    return this.queryStringParams[key];
};

本文标签: javascriptRetaining dropdown selection on page reloadStack Overflow