我有我编写的插件的这一部分,它允许玩家从下拉列表中选择一个消耗品以及他们想要使用的数量。最近有人要求他们稍后返回页面时所做的选择仍然存在。这听起来像是本地存储的工作!问题是当用户返回并且本地存储正确设置了该值时,它不会设置通常由 onchange 事件设置的 ConsumableNUM 的值。
这是当前的选择列表和按钮
<input type="button" class="btnFJ" value="Energy" onclick="ACTIONS.use_consumable(EnergyCons,ConsumableNUM)">
<form style="display: inline">
<SELECT class="FJselect" name="numberconsumables" style="width: 54px" id="FJsconsumables" onchange="ConsumableNUM = this.options[selectedIndex].value;">
<OPTION class="FJoptionBG1" selected value="1">1</OPTION>
<OPTION class="FJoptionBG2" value="2">2</OPTION>
<OPTION class="FJoptionBG1" value="3">3</OPTION>
<OPTION class="FJoptionBG2" value="4">4</OPTION>
<OPTION class="FJoptionBG1" value="5">5</OPTION>
<OPTION class="FJoptionBG2" value="6">6</OPTION>
<OPTION class="FJoptionBG1" value="7">7</OPTION>
<OPTION class="FJoptionBG2" value="8">8</OPTION>
<OPTION class="FJoptionBG1" value="9">9</OPTION>
<OPTION class="FJoptionBG2" value="10">10</OPTION>
<OPTION class="FJoptionBG1" value="11">11</OPTION>
<OPTION class="FJoptionBG2" value="12">12</OPTION>
<OPTION class="FJoptionBG1" value="13">13</OPTION>
<OPTION class="FJoptionBG2" value="14">14</OPTION>
<OPTION class="FJoptionBG1" value="15">15</OPTION>
<OPTION class="FJoptionBG2" value="16">16</OPTION>
<OPTION class="FJoptionBG1" value="17">17</OPTION>
<OPTION class="FJoptionBG2" value="18">18</OPTION>
<OPTION class="FJoptionBG1" value="19">19</OPTION>
<OPTION class="FJoptionBG2" value="20">20</OPTION>
<OPTION class="FJoptionBG1" value="21">21</OPTION>
<OPTION class="FJoptionBG2" value="22">22</OPTION>
<OPTION class="FJoptionBG1" value="23">23</OPTION>
<OPTION class="FJoptionBG2" value="24">24</OPTION>
<OPTION class="FJoptionBG1" value="25">25</OPTION>
<OPTION class="FJoptionBG2" value="26">26</OPTION>
<OPTION class="FJoptionBG1" value="27">27</OPTION>
<OPTION class="FJoptionBG2" value="28">28</OPTION>
<OPTION class="FJoptionBG1" value="29">29</OPTION>
<OPTION class="FJoptionBG2" value="30">30</OPTION>
<OPTION class="FJoptionBG1" value="50">50</OPTION>
<OPTION class="FJoptionBG2" value="100">100</OPTION>
<OPTION class="FJoptionBG1" value="500">500</OPTION>
</SELECT>
</form>
这里是调用先前选择的值的 localstorage 的设置。
document.getElementById("FJsconsumables").onchange = function() {
localStorage.setItem('FJsconsumables', document.getElementById("FJsconsumables").value);
}
if (localStorage.getItem('FJsconsumables')) {
document.getElementById("FJsconsumables").options[localStorage.getItem('FJsconsumables')].selected = true;
}
谢谢!
编辑
不知道为什么,但是,当我尝试将相同的方法应用于第二个选择列表时,它不起作用。它要么不保存价值,要么有时返回错误的价值!第二个列表及其功能如下:
<form style="display: inline">
<SELECT class="FJselect" name="typeofconsumables" style="width: 110px" id="FJConsumable">
<option>-Energy</option>
<OPTION class="FJoptionBG1b" value="1">Energy Drink - 10</OPTION>
<OPTION class="FJoptionBG1b" value="5">Energy Infusion - 20</OPTION>
<OPTION class="FJoptionBG1b" value="9">Atomic Energizer</OPTION>
<OPTION class="FJoptionBG1b" value="11">Nucleic Reaction - 50</OPTION>
<OPTION class="FJoptionBG1b" value="609">NUCLEIC ENERGY - 50</OPTION>
<OPTION class="FJoptionBG1b" value="234">Red Line - 55</OPTION>
<OPTION class="FJoptionBG2" value="523">SHC Present - 155</OPTION>
<OPTION class="FJoptionBG1" value="489">Hallow Skull - 160</OPTION>
<OPTION class="FJoptionBG2" value="499">Red Eyeball - 203</OPTION>
<OPTION class="FJoptionBG1" value="501">Blue Eyeball - 203</OPTION>
<OPTION class="FJoptionBG2" value="503">Yellow Eyeball - 203</OPTION>
</select>
和功能:
document.getElementById("FJConsumable").onchange = function() {
localStorage.setItem('FJConsumable', document.getElementById("FJConsumable").value);
}
if (localStorage.getItem('FJConsumable')) {
document.getElementById("FJConsumable").options[localStorage.getItem('FJConsumable')].selected = true;
}