在回发时保留选定的下拉选项

时间:2012-01-05 02:52:29

标签: javascript jquery drop-down-menu option selected

即使在页面刷新后如何保留从下拉列表中选择的内容,以便用户知道他/她使用jquery选择了什么?还是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>

2 个答案:

答案 0 :(得分:1)

试试这个:

<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]);
});

答案 1 :(得分:0)

<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>