鉴于以下 html 代码:
<body onload="loadSett()">
<form id="settingsForm" name="settingsForm">
<select name="Sound" id="Sound">
<option value="0">Off</option>
<option value="1">On</option>
</select>
<select name="Vibration" id="Vibration">
<option value="0">Off</option>
<option value="1">On</option>
</select>
</form>
</body>
我有以下 javascript :
var getSound;
var getVibra;
function loadSett() {
var form = $("#settingsForm");
getSound = localStorage.getItem("sound");
getVibra = localStorage.getItem("vibra");
if(getSound != undefined && getVibra != undefined) {
if(getSound == "1"){
document.getElementById('Sound').options[1].selected = true;
}
if(getVibra == "1"){
document.getElementById('Vibration').options[1].selected = true;
}
} else { alert("Empty"); }
}
问题:为什么选择标记的选项在 选择 时根据值加载页面存储在localStorage中?提前致谢
答案 0 :(得分:0)
代码的基础知识没有任何问题。也许检查控制台输出是否有错误等。
以下是您的代码的jsbin版本,可以按预期工作:http://jsbin.com/uwucib/1/edit