Removing items from the localStorage

后端 未结 4 1214
长发绾君心
长发绾君心 2021-01-13 14:54

I\'m trying to remove the individual items from the localStorage. I\'m using localStorage for storing selected list items in the listview. I attached the checkboxes for remo

相关标签:
4条回答
  • 2021-01-13 15:26

    try removing items like this, removing local storage items by their key values

    //syntax is localStorage.removeItem('keyName');
    //so if your key name is checked                  
    localStorage.removeItem('checked');
    

    See this Link for reading about local storage.

    0 讨论(0)
  • 2021-01-13 15:35

    Just use localStorage.clear();

    0 讨论(0)
  • 2021-01-13 15:39

    localstorage.removeItem() wont work as expected. I had faced the same problem while trying to handle the localstorage items.

    A work-around for this problem was to set the local storage item to null. It served my purpose.

    For example if you have a localStorage item named test then to clear the item use :

    localStorage.setItem("test", "");
    

    Although this may not be the correct way, i had gone bonkers over this issue and the above one served my purpose.

    0 讨论(0)
  • 2021-01-13 15:47

    I think the problem is in this part:

    $("#delete").on('click', function() {
                var checked = $('#add #check:checkbox:checked');
                var url = localStorage.key(checked);
                localStorage.removeItem(url);
                $("#favoritesList").listview('refresh');
    });
    

    you are not using correct key for deleting and its because you are using :

    var url = localStorage.key(checked);
    

    and checked==[Object]==1 so you are pointing to localStorage.key(1)


    for solving this problem you can both use key=localStorage.key(some);localStorage.removeItem(key); and localStorage.removeItem(some); but the important part is that you should match 'an index(first way)' or a 'key value(the second way)' for each checkbox and I can help you if I have the complete code (I think still there are some part of HTML or codes that are related)(also it would be so good if you put a fiddle of your codes)

    0 讨论(0)
提交回复
热议问题