简体   繁体   中英

retaining selected dropdown option on postback

How do I retain whatever was selected from the dropdown even after page refresh so user knows what he/she selected using jquery? or javascript?

<select id="hospitalDropDown" onchange="window.open(this.options[this.selectedIndex].value,'_top')"> 
      <option disabled="disabled">Select Hospital</option> 
      <option value="http://mysite.com/events/Pages/default1.aspx">All Hospitals</option>
      <option value="http://mysite.com/events/Pages/default1.aspx?hos=Dyer">Dyer</option>
      <option value="http://mysite.com/events/Pages/default1.aspx?hos=Carmel">Carmel</option>
    </select>

Try this:

<select id="hospitalDropDown" onchange="window.open('http://mysite.com/events/Pages/default1.aspx?hos='+this.value,'_top')"> 
  <option disabled="disabled">Select Hospital</option> 
  <option value="All">All Hospitals</option>
  <option value="Dyer">Dyer</option>
  <option value="Carmel">Carmel</option>
</select>

$(document).ready(function(){
    var value = window.location.href.match(/[?&]hos=([^&#]+)/) || [];
    $('#hospitalDropDown').val(value[1]);
});
<select id="hospitalDropDown"> 
    <option value="">All Hospitals</option>
    <option value="Dyer">Dyer</option>
    <option value="Carmel">Carmel</option>
</select>
<script type="text/javascript">

$(document).ready(function() {
    $('#hospitalDropDown').val('<?php echo $_GET['hos']; ?>');
    $('#hospitalDropDown').change(function() {
        location.href = 'http://mysite.com/events/Pages/default1.aspx?hos=' + $(this).val();
    });
});
</script>

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