简体   繁体   English

我如何使用带有两个下拉菜单的代码?

[英]How I can use that code with two dropdown menus?

How I can use that code with two dropdown menus?我如何使用带有两个下拉菜单的代码? Actually when I reload the page the first dropdown menu stays like I want but the second one no.实际上,当我重新加载页面时,第一个下拉菜单保持我想要的样子,但第二个没有。 Thanks.谢谢。 Sorry to ask, Im new in all this incredible world!很抱歉问,我是这个令人难以置信的世界的新手!

var init = function () { strong text var sel = $("select"), but = $("button"); var init = function () { 强文本 var sel = $("select"), but = $("button");

    var clearSelected = function () {
      sel.find(":selected").prop("selected", false);
    };
  
    if (localStorage.getItem("pref")) {
      var pref = localStorage.getItem("pref");
      clearSelected();
      //set the selected state to true on the option localStorage remembers
      sel.find("#" + pref).prop("selected", true);
    }
  
    var setPreference = function () {
      //remember the ID of the option the user selected
      localStorage.setItem("pref", sel.find(":selected").attr("id"));
    };
  
    var reset = function () {
      clearSelected();
      localStorage.setItem("pref", undefined);
    };
  
    sel.on("change", setPreference);
    but.on("click", reset);
  };
  $(init);
  **strong text**

I think .filter is what your need here.我认为.filter是你需要的。 See this看到这个
.find will return the first element it founds that responds to the criteria you give. .find将返回它找到的第一个响应您提供的条件的元素。 That's why your first dropdowm list is correct and not the second.这就是为什么你的第一个 dropdowm 列表是正确的,而不是第二个。
Also, for your storage, I guess you have 2 choices:另外,对于您的存储,我想您有两种选择:

  • Set an item for the first dropdown menu, and another one for the second one that you can handle separatly.为第一个下拉菜单设置一个项目,为您可以单独处理的第二个下拉菜单设置另一个项目。
  • Set only one item but containing an array of the values selected.仅设置一项但包含所选值的数组。 With this solution you would have to set the item with an object as so使用此解决方案,您必须像这样使用对象设置项目

Hope I helped.希望我有所帮助。

EDIT编辑

I think this should do the math我认为应该算术

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

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