1

我正在尝试检查 url 中是否有 hos 参数是否有任何内容,如果有,则将该值作为 selected 属性传递给页面刷新时的下拉列表,因此即使在刷新后下拉选项仍保持选中状态

   var value = window.location.href.match(/[?&]hos=([^&#]+)/) || [];

        if (value.length == 2) {
            $('#hospitalDropDown[value="' + value[1] + '"]').attr('selected', 'selected');

    }

这是下拉列表:

<select id="hospitalDropDown" onchange="window.open(this.options[this.selectedIndex].value,'_top')">          <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> 
4

1 回答 1

0

看起来您的选项将查询字符串(All Hospitals, Dyer, Carmel)作为 . text,但整个 url 作为value.

结果,匹配您的value选项*=

if (value.length == 2) {
    $('#hospitalDropDown option[value*="' + value[1] + '"]').attr("selected", "selected");
于 2012-01-05T16:10:45.803 回答