【问题标题】:Phonegap - select options "selected" via JavaScriptPhonegap - 通过 JavaScript 选择“选择”选项
【发布时间】:2013-01-28 06:36:06
【问题描述】:

给定以下 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"); }
}

问题:为什么select标签的option没有selected根据值加载页面存储在本地存储?提前致谢

【问题讨论】:

    标签: cordova selection local-storage


    【解决方案1】:

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

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

    【讨论】:

    • 我检查了本地存储,“声音”的值为 0,“振动”的值为 1...但在加载页面时,两者都是“关闭”,即 0
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多