2013-06-22 24 views
1

我有一个表单,它将用户的优先级保存在本地存储中,它可以在this fiddle or below中看到。选择多个值的选择框的HTML5本地存储存储表单数据

我到目前为止有两个主要问题。

  1. 单击保存按钮时,您将清空myStorage,然后追加刚选择的内容,以便他们可以看到他们点击的结果。它只记得你是否重新演奏小提琴。
  2. 我最大的问题是,我有很多选择字段有一个选项,但在这种情况下,用户可以选择多个,所以我需要添加到它,以便用户可以将多个值保存到本地存储所以下次他们加载页面时会保存多个选择?

    <form name="Filter"> 
    <select multiple="1" id="filter"> 
        <option value="111">Andrew</option> 
        <option value="222">Bill</option> 
        <option value="333">Charles</option> 
        <option value="444">Dikembe</option> 
        <option value="555">Edward</option> 
    </select> 
    </form> 
    <div id="store">click to store</div> 
    <br> 
    <h3>People I have stored</h3> 
    
    <div id="myStorage"></div> 
    

JS

var iSelectedTitle = localStorage.getItem('title'); 
    var iSelectedVal = localStorage.getItem('value'); 
    console.log("iSelectedTitle: " + iSelectedTitle); 
    console.log("iSelectedVal: " + iSelectedVal); 

    $("#filter option").each(function() { 
     if ($(this).val() == iSelectedVal) { 
      $(this).attr("selected", "selected"); 
     } 
    }); 

    $("#store").click(function() { 
     var mytitle = $("#filter option:selected").text(); 
     var storedTitle = localStorage.setItem("title", mytitle); 
     console.log("mytitle: " + mytitle); 
     console.log("storedTitle: " + storedTitle); 

     var myValue = $("#filter option:selected").val(); 
     var storedValue = localStorage.setItem("value", myValue); 
     console.log("myValue: " + myValue); 
     console.log("storedValue: " + storedValue); 

     if (iSelectedTitle != "undefined") { 
      $('#myStorage').empty().append(iSelectedTitle); 
     } 
    }); 
    if (iSelectedTitle != "undefined") { 
     $('#myStorage').append(iSelectedTitle + ' - <a target= "_blank "href="http://www.example.com/' + iSelectedVal + '">View profile</a>'); 
    } 

回答

2

后,您可以添加多个选项,使用map jQuery的数组:

var optArray = $("#filter option:selected").map(function() { 
     return { 
      "title": this.innerHTML, 
      "value": this.value 
     } 
}).get(); 

这会给你一个漂亮的阵列是这样的:

[ 
    { "title": "Andrew", "value": "111" }, 
    { "title": "Bill", "value": "222" }, 
    { "title": "Charles", "value": "333" } 
] 

用于添加到localStorage的:

$("#store").click(function() { 
    //get the selected options in the form of an array 
    var optArray = $("#filter option:selected").map(function() { 
     return { 
      "title": this.innerHTML, 
      "value": this.value 
     } 
    }).get(); 
    console.log(optArray); 
    //set that to localStorage 
    localStorage["optArray"] = JSON.stringify(optArray); 
    //refresh myStorage 
    getFromStore(); 
}); 

对于刷新与新添加的人myStorage容器,你必须把这种处理器的`点击事件(上图)中的最后一个事件。

var getFromStore = function() { 
    //check if store values are null, if null, make store =[] 
    var store = [undefined, null].indexOf(localStorage["optArray"]) != -1 ? [] : JSON.parse(localStorage["optArray"]); 
    console.log(store); 
    //empty container before u put values 
    $('#myStorage').html(''); 
    //check if store is empty 
    if (store.length != 0) { 
     //loop over store if it aint empty and append the content into myStorage div 
     for (var k in store) { 
      var str = '<div>' + store[k]["title"] + ' - <a target= "_blank" href="http://www.example.com/' + store[k]["value"] + '">View profile</a></div>'; 
      console.log(store[k]); 
      $('#myStorage').append(str); 
     } 
    } else { 
     //do this if no data is found in localStorage 
     $("#myStorage").html("No people found in store"); 
    } 
} 

然后,在DOM ready,叫getFromStore刷新myContainer在页面加载:

$(function() { 
    getFromStore(); 
}) 

演示:http://jsfiddle.net/hungerpain/hqVGS/9/

编辑

要选择默认的复选框,在中添加下面的行0功能:

$("[value=" + store[k]["value"] + "]","#filter").prop("selected", true); //find the option with the corresponding value and select it 

更新演示:http://jsfiddle.net/hungerpain/hqVGS/10/

+0

这看起来很棒,谢谢唯一需要从原件中添加的东西,那就是当您再次运行小提琴时,应该在选择字段中选择已存储的人。 – ak85

+0

会做..当然:) – krishgopinath

+0

你可以删除upvote现在明天做?我认为我达到了我每天的声望上限。如果你upvote明天il得到我的10分:) – krishgopinath

1

你可以在一个阵列保存多个值,

var myValuesArr = ['one','two','three']; 

的阵列将需要之前将其保存到localStorage

被序列化
var serialVals = JSON.stringify(myValuesArr); 
localStorage.setItem('numbers',serialVals); 

同样,存储的数据将不得不去系列化它读回

var serialVals = localStorage.getItem('numbers'); 
var myValuesArr = JSON.parse(serialVals); 
+0

感谢您的建议,我曾透过JSON.stringify尝试过,但是当我这样做,例如,而不是返回,安德鲁,我返回andrewbill如果我选择的第一个2个选项。我认为这可能是这样做的方式,但我不知道如何分割数据,以便可以在加载时标记值? – ak85

+0

@ ak85尝试存储与id关联的名称,即。使用Object而不是Array:'{'111':'Andrew',...}'。 – K3N