javascript - LocalStorage - 下拉菜单
问题描述
我有问题,本地存储保存第一个下拉菜单选项,但不是第二个和第三个。应该在 HTML 和 JS 中编辑或添加多个内容。
var init = function (){
//an ugly warning to users without localStorage support
if(!window.localStorage){
$('body').prepend('Sorry, you browser does not support local storage');
return false;
}
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);
};
$(document).ready(init);
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="2.js"></script>
</head>
<select>
<option selected></option>
<option id="opt1">1</option>
<option id="opt2">2</option>
<option id="opt3">3</option>
<option id="opt4">4</option>
</select>
<br></br>
<select>
<option selected></option>
<option id="opt5">5</option>
<option id="opt6">6</option>
<option id="opt7">7</option>
<option id="opt8">8</option>
</select>
解决方案
我进行了更改,但是现在它们都没有保存。我为我的知识水平道歉,但这是我的第一次,只是需要它来工作。
var init = function (){
//an ugly warning to users without localStorage support
if(!window.localStorage){
$('body').prepend('Sorry, you browser does not support local storage');
return false;
}
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', this.value);
};
var reset = function(){
clearSelected();
localStorage.setItem('pref', undefined);
}
sel.on('change', setPreference);
but.on('click', reset);
};
$(document).ready(init);
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="2.js"></script>
</head>
<select id="first">
<option id="opt1">1</option>
<option id="opt2">2</option>
<option id="opt3">3</option>
<option id="opt4">4</option>
</select>
<br></br>
<select id="second">
<option id="opt5">5</option>
<option id="opt6">6</option>
<option id="opt7">7</option>
<option id="opt8">8</option>
</select>
<br></br>
推荐阅读
- nginx - Nuxtjs 失败:WebSocket 握手期间出错:意外响应代码:500
- swift - SwiftUI ObjectBinding 不会使用 combine 接收来自可绑定对象的 didchange 更新
- jsf - 将其他对象传递给 commandLink
- python - (Python)如何将包含日期和时间的列(例如 2019-07-02 00:12:32 UTC)拆分为两个单独的列?
- java - Flink kafka 源在瞬态失败后停止提交偏移量
- jquery - 表格顶部的样式数据表行
- nginx - 如何实现低延迟 HLS?
- autodesk-forge - 是否可以使用 Autodesk Forge 模型衍生 API 为可视项设置自定义缩略图?
- javascript - 当用户滚动(不是点击!)时如何自动滚动(向上和向下)到部分
- python - 如何匹配多列并在熊猫中查找特定列值?