我正在尝试从 localStorage 中删除单个项目。我正在使用 localStorage 在列表视图中存储选定的列表项。我附加了用于从本地存储中删除所选项目的复选框。但是,当我删除选定的复选框时,它不会像我选择一个复选框时那样正确删除,它删除的两个或三个项目正在从 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"> <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');
});
}