gpt4 book ai didi

javascript - localStorage 不适用于 html5 中的所有字段?

转载 作者:行者123 更新时间:2023-12-03 02:54:09 24 4
gpt4 key购买 nike

大家好,我正在开发 codeigniter 项目,在我的表单中,我使用 localstorage 来存储选定的值并在页面加载后显示。但不知怎的,它并不适用于我的所有领域。有时第一个值有效,有时最后一个值有效。我总共有 4 个字段,所有值都应存储在本地存储中。

HTML 部分:

 <div class="control-group">
<label class="control-label">Select Month</label>
<div class="controls">
<select id="month" name="month">
<option value="">--Select Option--</option>
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label">Select Year</label>
<div class="controls">
<select id="year" name="year">
<option value="">--Select Option--</option>
<option>2017</option>
<option>2016</option>
<option>2015</option>
<option>2014</option>
<option>2013</option>
<option>2012</option>
<option>2011</option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label">Select Employee</label>
<div class="controls">
<select id="employee" name="employee_name">
<option value="">--Select Option--</option>
<?php
foreach ($salary as $row) {?>
<option value='<?php echo $row->EMPLOYEE_ID;?>'><?php echo $row->EMPLOYEE_NAME;}?></option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label">Select Country</label>
<div class="controls">
<select id="country" name="org_id">
<option value="">--Select Option--</option>
<option value="40">UK</option>
<option value="41">INDIA</option>
<option value="47">POLAND</option>
<option value="57">GERMANY</option>
</select>
</div>
</div>

到目前为止,只有第一个字段(月份)正在工作。在表单加载时,仅将其选定的值存储在本地存储中。其余部分在下拉列表中显示空白。JavaScript 代码:

  <script type="text/javascript">
var editingArea = document.getElementById("month");
var editingArea1 = document.getElementById("year");
var editingArea2 = document.getElementById("employee");
var editingArea3 = document.getElementById("country");

var KEY = "storageKey";
var text = localStorage.getItem(KEY);
if(text !== null){
editingArea.value = text;
}
var KEY = "storageKey";
var text1 = localStorage.getItem(KEY);
if(text1 !== null){
editingArea1.value = text1;
}
var KEY = "storageKey";
var text2 = localStorage.getItem(KEY);
if(text2 !== null){
editingArea2value = text2;
}
var KEY = "storageKey";
var text3 = localStorage.getItem(KEY);
if(text3 !== null){
editingArea3.value = text3;
}

// whenever the textarea is updated, store the contents also into the storage
editingArea.addEventListener("click", function(event){
localStorage.setItem(KEY,editingArea.value);
},false);
editingArea1.addEventListener("click", function(event){
localStorage.setItem(KEY,editingArea1.value);
},false);
editingArea2.addEventListener("click", function(event){
localStorage.setItem(KEY,editingArea2.value);
},false);
editingArea3.addEventListener("click", function(event){
localStorage.setItem(KEY,editingArea3.value);
},false);


var resetButton = document.getElementById("reset");
resetButton.addEventListener("click", function(event){
localStorage.setItem(KEY,"");
editingArea.value = "";
editingArea1.value = "";
editingArea2.value = "";
editingArea3.value = "";
},false);


</script>

最佳答案

您对所有字段使用相同的car KEY = "storage Key";。对各个字段使用不同的

关于javascript - localStorage 不适用于 html5 中的所有字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47708074/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com