从localStorage删除项目 [英] Removing items from the localStorage

查看:74
本文介绍了从localStorage删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从localStorage中删除单个项目.我正在使用localStorage在listview中存储选定的列表项.我附加了用于从本地存储中删除所选项目的复选框.但是,当我删除选定的复选框时,其删除不正确,就像我选中一个复选框时一样,其删除的两个或三个项目正在从localStorage中删除.

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 removing the selected items from the localstorage. But when I'm removing the selected checkboxes its not deleting properly like when I selected one checkbox its deleting two or three items are deleting from the localStorage.

HTML代码:

<!-- Favourites page -->
<div data-role="page" id="fav">
     <div data-role="header" data-position="fixed">
    </div>
    <div data-role="content" class="ulDiv">
    <ul data-role="listview" data-split-icon="minus" data-split-theme="d" data-filter="true"data-icon="false" data-filter-placeholder="Search" id="favoritesList">
            </ul>
    </div>
    <div data-role="footer" data-position="fixed" class="my-footer">
    </div>
</div>

我的Js代码:

 $(document).ready( function() {
    $("#section_list").on('click', 'a', function() {
                var item = $(this).find('h2').text();
                var desc = $(this).find('p').text();
                var html = '<p></br><p class="description">' + desc + '</p>';
                $('.addToFavoritesDiv').click(function() {
                    var url = $(location).attr('href');
                    if (!supportLocalStorage())
                    {
                        item = 'No support';
                    }
                    else
                    {
                        try
                            {
                                localStorage.setItem('autosave' + url, item + html);

                            }
                        catch (e)
                            {
                                if (e == QUOTA_EXCEEDED_ERR)
                                {
                                    alert('Quota Exceeded');
                                }
                            }   
                    }
                });
    });

    $('#fav').click(function() {
        fromStorage();
        $("#favoritesList").listview('refresh');
    }); 

    $(document).on('click', '#edit', function() {
        fromGetStorage();
    });
});

function supportLocalStorage()
{       
    return typeof(Storage) !== 'undefined';
}

function fromStorage()
{
    $("#favoritesList").empty();
    $("#favoritesList").listview('refresh');
    for (var i = 0; i < localStorage.length; i++) {
        var url = localStorage.key(i);
            var item = localStorage.getItem(url);
            $("#favoritesList").append('<li id="add"><a href="' + url + '" style="text-decoration:none;color:black">' + item  + '</a></li>').attr('url', url);
    }
    $("#favoritesList").listview('refresh');
}

function fromGetStorage() 
    {   
     $("#favoritesList").empty();  
     $("#favoritesList").append('<input type="checkbox" name="all" id="select" />Select all</br>');
     $("#fav .ui-listview-filter").remove();
        for (var i = 0; i < localStorage.length; i++) {
        var url = localStorage.key(i);
         var item = localStorage.getItem(url);
          $("#favoritesList").append('<li id="add"><div><input type="checkbox" name="check" id="check">&nbsp;<a href="' + url + '" style="text-decoration:none;color:black">' + item + '</a></div></li>').attr('url', url);
        $("#favoritesList").listview('refresh');
 }

$("#select").click(function(event){
    event.stopPropagation();
});
    $('#select').change(function() {
    if($("#select").is(':checked')) {
        $("#add #check").prop("checked", true);
        $('#empty').on('click', function() {
            localStorage.clear();
        $("#favoritesList").listview('refresh');
        });
    }
    else {
        $("#add #check").prop("checked", false);
    }
 });


$("#add #check").click(function(event){
    event.stopPropagation();
});

    $("#add #check").change(function() {
        var chkLength = $('input[name="check"]:checkbox').length;
        var chkdLength = $('input[name="check"]:checkbox:checked').length;
        if (chkLength == chkdLength)
        {
            $('#select').prop('checked', true);
        }
        else 
        {
            $('#select').prop('checked', false);
        }

});

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

推荐答案

尝试删除此类项目,并根据其键值删除本地存储项目

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.

这篇关于从localStorage删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆