我有一部分我写的插件,让玩家从下拉列表中选择一个消耗品,以及他们想要使用多少。最近要求当他们稍后返回页面时,他们所做的选择仍然存在。这听起来像是本地存储的工作!问题是当用户返回并且localstorage正确设置了值时,它不会设置通常由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;
}
答案 0 :(得分:0)
将选项设置为选中状态不会触发onchange处理程序。这就是您未定义全局变量的原因。明确设置一个值:
if (localStorage.getItem('FJsconsumables')) {
document.getElementById("FJsconsumables").options[localStorage.getItem('FJsconsumables')].selected = true;
ConsumableNUM = localStorage.getItem('FJsconsumables');
} else {
ConsumableNUM = "1"; // or whatever is a usefull default value here
}
对于你的第二个问题:你有一个没有价值的选项,这可能会有所不同。试着放下它以确保。