根据下拉值设置LocalStorage

时间:2015-03-09 14:50:20

标签: javascript html

如何根据下拉选择的值设置localstorage项的值?

   <select id="theme" onchange="">
            <option value="simple">Simple</option>
            <option value="black">Black</option>
            <option value="blood">Blood</option>
            <option value="beige">Beige</option>
            <!-- <option value="default">Default</option> -->
            <option value="league">League</option>
            <option value="moon">Moon</option>
            <option value="night">Night</option>
            <option value="serif">Serif</option>
            <option value="sky">Sky</option>
            <option value="solarized">Solarized</option>
        </select>

4 个答案:

答案 0 :(得分:1)

您可以像这样在变更处理程序上设置值:

var selectTheme = document.getElementById('theme');

selectTheme.addEventListener('change', function(){
    localStorage.setItem('theme', this.value);
});

或者只是简单

localStorage.setItem('theme', selectTheme.value);

答案 1 :(得分:0)

var e = document.getElementById("my-select");
var option = e.options[e.selectedIndex].value;
localStorage['value'] = option;
console.log('The selected value is: ' + localStorage['value']);

答案 2 :(得分:0)

这似乎现在有效

function setTheme(themeOp){
    var Themelink = document.getElementById('ppt').contentWindow.document.getElementById('theme');


var e = document.getElementById("theme");
var option = e.options[e.selectedIndex].value;
localStorage['value'] = option;
console.log('The selected value is: ' + localStorage['value']);

    console.log (localStorage.theme);
    if (Themelink){
        Themelink.setAttribute('href','http://myurl/css/theme/' + localStorage['value'] + '.css'); 
    }
}

答案 3 :(得分:0)

这对我有用

 <select id="theme" onchange="">
        <option value="simple">Simple</option>
        <option value="black">Black</option>
        <option value="blood">Blood</option>
        <option value="beige">Beige</option>
        <!-- <option value="default">Default</option> -->
        <option value="league">League</option>
        <option value="moon">Moon</option>
        <option value="night">Night</option>
        <option value="serif">Serif</option>
        <option value="sky">Sky</option>
        <option value="solarized">Solarized</option>
    </select>

这里是jquery

$(function(){

    $('#theme').change(function() {
      var locationName = $('#theme option:selected').val();
      localStorage.setItem('locationName', locationName);

    });
});