保持下拉选择?我究竟做错了什么?

时间:2012-01-05 16:08:15

标签: javascript jquery selection dropdownbox

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

   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> 

1 个答案:

答案 0 :(得分:0)

看起来您的选项包含查询字符串 - 所有医院,Dyer,Carmel-为text,但整个网址为value

因此,您的选项value*=匹配

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