简体   繁体   English

从 URL 地址自动预选下拉值

[英]Auto pre-select dropdown value from URL address

I'm using the dropdown select menu which redirects users to selected cities.我正在使用将用户重定向到选定城市的下拉选择菜单。 I have searched for this topic everywhere and tried many solutions found on stackoverflow but each of them did not work.我到处搜索这个话题,并尝试了很多在 stackoverflow 上找到的解决方案,但每个解决方案都不起作用。 In many cases it even disabled the redirection of my dropdown.在许多情况下,它甚至禁用了我的下拉菜单的重定向。 So I am posting a new question.所以我发布了一个新问题。 Hopefully that someone could solve my problem.希望有人能解决我的问题。

Problem: When I visit URL I see select delivery city - non value option.问题:当我访问 URL 时,我看到选择交货城市 - 非价值选项。 It should show the selected city based on URL address.它应该根据 URL 地址显示选定的城市。

My URL looks like this /kategoria-produktu/CITY U SELECT (/kategoria-produktu/cadca/)我的 URL 看起来像这样 /kategoria-produktu/CITY U SELECT (/kategoria-produktu/cadca/)

To sum up: When u visit url /kategoria-produktu/cadca the dropdown should be preselect on current url and display Čadca.总结:当您访问 url /kategoria-produktu/cadca 时,下拉列表应该在当前 url 上预选并显示 Čadca。

Any ideas how could I solve this?任何想法我怎么能解决这个问题? Thank you very much!非常感谢!

CODE代码

JS JS

if(location.href.indexOf(localStorage.country) == -1){
location.href = localStorage.country
}

function formChanged(form) {
var val = form.options[form.selectedIndex].value;
if (val !== 'non-value') {
if (localStorage) {
localStorage.country = val;
}

if (!location.href.indexOf(val)) {    
location = val;
   }
  }
 }

HTML HTML

 <form name="form1">
 <select id="saleTerm" onchange="formChanged(this); location = 
 this.options[this.selectedIndex].value;" NAME="country" SIZE="1">
 <OPTION VALUE="non-value">Select delivery city</option>
 <OPTION VALUE="/kategoria-produktu/cadca/">Čadca</option>
 <OPTION VALUE="/kategoria-produktu/brno/">Brno</option>
 <OPTION id="bratislava" VALUE="/kategoria-produktu/bratislava/">Bratislava</option>
 </select>
 </form>

So a bunch of little things need to change here for you to get what you want.所以这里有很多小事情需要改变,你才能得到你想要的。 I'll try to write them all down:我会试着把它们都写下来:

  1. You should access localStorage using getItem and setItem like in the localStorage MDN documentation您应该像在localStorage MDN 文档中一样使用 getItem 和 setItem 访问 localStorage

  2. Use an event listener instead of the inline onchange attribute, it's much cleaner.使用事件侦听器而不是内联onchange属性,它更简洁。

  3. You probably want to use includes instead of indexOf since you are looking for a substring (country) in a string (href), indexOf won't do this for you.您可能想要使用包含而不是 indexOf,因为您要在字符串 (href) 中查找子字符串(国家/地区),indexOf 不会为您执行此操作。

  4. I used location.pathname since you really only care about the path, there are better ways to get the exact path parameter you want.我使用location.pathname因为你真的只关心路径,有更好的方法来获得你想要的确切路径参数。

  5. No need to use a <form/> as far as I can see from the code you shared.就我从您共享的代码中看到的而言,无需使用<form/>

  6. I removed /kategoria-produktu/ from the option's value attribute since its repetitive and just placed it once in the js我从选项的 value 属性中删除了/kategoria-produktu/因为它是重复的,并且只在 js 中放置了一次

  7. You should change the value of the select to the city you want as the default selected.您应该将select的值更改为您想要作为默认选择的城市。 You can do this by parsing out the city from the path and setting it as the value attribute on the select您可以通过从路径中解析出城市并将其设置为 select 上的value属性来完成此操作

I think that's it, here is an example using those points above.我想就是这样,这是一个使用上述要点的示例。

 const PREFIX = "kategoria-produktu"; window.addEventListener('load', function() { let countryInStorage = localStorage.getItem("country"); if (countryInStorage && !location.pathname.includes(countryInStorage)) { location.href = `/${PREFIX}/${countryInStorage}`; } document.getElementById("saleTerm").addEventListener("change", formChanged); setDefaultOption(); }) function setDefaultOption() { let countryPath = location.pathname.split("/")[2]; if (countryPath) { document.getElementById("saleTerm").value = countryPath; } } function formChanged() { let selectedCountry = this.value; if (selectedCountry !== "non-value") { if (localStorage) { localStorage.setItem("country", selectedCountry); } if (!location.pathname.includes(selectedCountry)) { location.href = `/${PREFIX}/${selectedCountry}`; } } }
 <select id="saleTerm" name="country"> <option value="non-value">Select delivery city</option> <option value="cadca">Čadca</option> <option value="brno">Brno</option> <option value="bratislava">Bratislava</option> </select>

If I understand it correctly, you are looking onto showing the proper option from the select element based on the URL.如果我理解正确,您正在查看基于 URL 的 select 元素中的正确选项。

Look at the example below.看看下面的例子。 It basically runs a process on page load and when the DOM is ready (hence DOMContentLoaded ) to check if an option based on URL exists in the select options and picks that.它基本上在页面加载时运行一个过程,当 DOM 准备好时(因此DOMContentLoaded )检查选择选项中是否存在基于 URL 的选项并选择它。 You may have to update your logic depending on the URL structure.您可能需要根据 URL 结构更新逻辑。 The example below assumes your URL is always formatted like http://your.domain.com/kategoria-produktu/<city>/ .下面的示例假设您的 URL 的格式始终为http://your.domain.com/kategoria-produktu/<city>/

document.addEventListener("DOMContentLoaded", function() {
  // find the option based on the URL.
  let option = document.querySelector("#saleTerm > option[value='" + location.pathname + "']");

  // assign the option value to the select element if such exists.
  if (option) {
    document.querySelector("#saleTerm").value = option.value;
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM