我试图检查 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>
最佳答案
看起来您的选项将查询字符串值(所有医院、戴尔、卡梅尔)作为文本
,但将整个网址作为值
.
因此,将选项的值
与*=
相匹配
if (value.length == 2) {
$('#hospitalDropDown option[value*="' + value[1] + '"]').attr("selected", "selected");
关于javascript - 保留下拉选择吗?我究竟做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8745678/