如何使用 JQuery 删除特定的 sessionStorage 值
How to delete a specific sessionStorage value using JQuery
我想知道我是否可以删除特定的 sessionStorage 值,如果可以,我如何使用 JQuery 完成此操作?到目前为止,我可以删除输入字段,但我似乎无法删除 sessionStorage 值。下面列出了我的代码。
这是我的 jsfiddle https://jsfiddle.net/fj324rzg/1/
HTML
<ul>
<li>
<div class="worker-container-last">
<label class="worker-label">
<select name="title[]" class="title">
<option value="Select a Title" selected="selected">Select a Title</option>
<option value="Boss">Boss</option>
<option value="Worker">Worker</option>
<option value="Manager">Manager</option>
</select>
</label>
</div>
<div class="add-more"></div>
<div><a class="worker" title="" href="">Add Another Worker</a></div>
</li>
</ul>
JQuery
var worker_record = [];
$(document).ready(function(){
workerData();
$('.worker').on('click', function(e){
e.preventDefault();
e.stopPropagation();
var title = $('.title:first').val();
var someData = {title: title};
worker_record.push(someData);
sessionStorage.worker = JSON.stringify(worker_record);
workerData();
});
function workerData(){
var max_fields = 6;
var x = 1;
if(!(typeof sessionStorage.worker === 'undefined' || sessionStorage.worker.length<1)){
worker_record = JSON.parse(sessionStorage.worker);
}
$('.add-more').empty();
for(var i=0; i<worker_record.length; i++){
if(x < max_fields){
x++;
var title = worker_record[i].title;
var worker = '<div><div class="delete-worker"><a title="" href="#">Delete</a></div><select name="title[]" class="title title-options"><option value="Select a Title" selected="selected">Select a Title</option><option value="Boss">Boss</option><option value="Worker">Worker</option><option value="Manager">Manager</option></select></div>';
$('.add-more').append(worker);
$('.title:eq('+$('.title-options').length+')').val(title);
}
}
$('.title:first').val('Select a Title');
}
$('.add-more').on('click', '.delete-worker', function(e){
e.preventDefault();
e.stopPropagation();
$(this).parent('div').remove(); x--;
});
});
当 removEvent.You 只是删除下面的 dom content.Refer 以从 Sessionstorage
中删除工作人员记录时,您没有删除工作人员中的记录
worker_record = JSON.parse(sessionStorage.worker);
alert(worker_record);
worker_record.splice(worker_record.length -1, 1);// Record index passed as 2nd paramter
alert(worker_record);
sessionStorage.worker = JSON.stringify(worker_record);
我想知道我是否可以删除特定的 sessionStorage 值,如果可以,我如何使用 JQuery 完成此操作?到目前为止,我可以删除输入字段,但我似乎无法删除 sessionStorage 值。下面列出了我的代码。
这是我的 jsfiddle https://jsfiddle.net/fj324rzg/1/
HTML
<ul>
<li>
<div class="worker-container-last">
<label class="worker-label">
<select name="title[]" class="title">
<option value="Select a Title" selected="selected">Select a Title</option>
<option value="Boss">Boss</option>
<option value="Worker">Worker</option>
<option value="Manager">Manager</option>
</select>
</label>
</div>
<div class="add-more"></div>
<div><a class="worker" title="" href="">Add Another Worker</a></div>
</li>
</ul>
JQuery
var worker_record = [];
$(document).ready(function(){
workerData();
$('.worker').on('click', function(e){
e.preventDefault();
e.stopPropagation();
var title = $('.title:first').val();
var someData = {title: title};
worker_record.push(someData);
sessionStorage.worker = JSON.stringify(worker_record);
workerData();
});
function workerData(){
var max_fields = 6;
var x = 1;
if(!(typeof sessionStorage.worker === 'undefined' || sessionStorage.worker.length<1)){
worker_record = JSON.parse(sessionStorage.worker);
}
$('.add-more').empty();
for(var i=0; i<worker_record.length; i++){
if(x < max_fields){
x++;
var title = worker_record[i].title;
var worker = '<div><div class="delete-worker"><a title="" href="#">Delete</a></div><select name="title[]" class="title title-options"><option value="Select a Title" selected="selected">Select a Title</option><option value="Boss">Boss</option><option value="Worker">Worker</option><option value="Manager">Manager</option></select></div>';
$('.add-more').append(worker);
$('.title:eq('+$('.title-options').length+')').val(title);
}
}
$('.title:first').val('Select a Title');
}
$('.add-more').on('click', '.delete-worker', function(e){
e.preventDefault();
e.stopPropagation();
$(this).parent('div').remove(); x--;
});
});
当 removEvent.You 只是删除下面的 dom content.Refer 以从 Sessionstorage
中删除工作人员记录时,您没有删除工作人员中的记录worker_record = JSON.parse(sessionStorage.worker);
alert(worker_record);
worker_record.splice(worker_record.length -1, 1);// Record index passed as 2nd paramter
alert(worker_record);
sessionStorage.worker = JSON.stringify(worker_record);