保留下拉选择吗?我做错了什么?

发布于 2024-12-25 05:24:02 字数 797 浏览 3 评论 0原文

我试图检查 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> 

I am trying to check if there is hos paramaeter in the url has anything and if there is, then pass that value as the selected attribute to the dropdown on page refresh, so the dropdown option remains selected even after refresh

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

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

    }

Here is the dropdown:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏の忆 2025-01-01 05:24:02

看起来您的选项将查询字符串(所有医院、戴尔、卡梅尔)作为文本,但将整个网址作为

因此,将选项的 value*= 进行匹配

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

It looks like your options have the querystring value—All Hospitals, Dyer, Carmel—as the text, but the whole url as the value.

As a result, match on your option's value with *=

if (value.length == 2) {
    $('#hospitalDropDown option[value*="' + value[1] + '"]').attr("selected", "selected");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文