0

给定以下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中存储的值在加载页面时没有选择select标签的选项?提前致谢

4

1 回答 1

0

您的代码基础没有问题。也许检查控制台输出是否有错误等。

这是您的代码的 jsbin 版本,可以按预期工作:http: //jsbin.com/uwucib/1/edit

于 2013-01-27T22:36:06.677 回答