0
我想知道如果我可以刪除特定的sessionStorage值,那麼我該如何使用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--;
});
});
現在我得到以下錯誤'未捕獲的ReferenceError:x未定義,否則一切都很好。 – obi
您無法訪問函數外部的x。 你可以得到索引刪除Dom並通過相同的拼接參數 – tsb
現在,當我刪除一個輸入我不能添加另一個輸入。您可以添加 – obi