用户从选择框中选择的值设置为按钮值,但它的(LocalStorage)值在页面刷新时更改(F5)
function change(){
var opt = $("#gsb_tb option:selected").text();
var stored = localStorage.setItem('project',JSON.stringify(opt));
var valueofstore= JSON.parse(localStorage.getItem('project'));
document.getElementById("set-project").value= valueofstore;
}
<select id="gsb_tb" style="height: 45px; width: 78%; margin-left: 70px; padding-right: 90px; vertical-align: middle;" tabindex="0" autocomplete="off">
<option value="" disabled selected>Select your Project</option>
<option value="370"> E-Card</option>
<option value="278">Labs</option>
<option value="196">Absolute DB</option>
<input value="Set as Working Project" onclick="change()" aria-label="I'm Feeling Lucky" name="btnI" class='center-btn' jsaction="sf.lck" type="submit" id='set-project'>
最佳答案
据我了解,您可能正在寻找window.onload
function change(){
var opt = $("#gsb_tb option:selected").text();
var stored = localStorage.setItem('project',JSON.stringify(opt));
var valueofstore= JSON.parse(localStorage.getItem('project'));
console.log(valueofstore)
document.getElementById("set-project").value= valueofstore;
}
function load(){
var valueofstore= JSON.parse(localStorage.getItem('project'));
console.log(valueofstore)
if (valueofstore){
document.getElementById("set-project").value= valueofstore;
}
}
window.onload = load;