[英]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:另外,对于您的存储,我想您有两种选择:
Hope I helped.希望我有所帮助。
EDIT编辑
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.