我的应用程序按预期工作,直到我添加了存储在localStorage中的项目的“DELETE”方法。停止替换localStorage中已存在的键和值的jQuery
我有这样的HTML代码,需要一个字符串:
<div>
<div ><input type="text" id="manuallyName" name="manuallyName" placeholder="Medicine Name, Strength & Form..." /></div>
<div class="ui-grid-a" style="padding:15px;">
<div class="ui-block-a"><input id="manualClear" type="Button" data-icon="delete" data-iconpos="left" value="Cancel" /></div>
<div class="ui-block-b"><input id="manuallyAdd" type="button" data-icon="cross" value="Add" /></div>
</div>
</div>
当“添加”按钮被按下下面的脚本被称为:
$(document).ready(function() {
$('#manuallyAdd').click(function() {
if ($("#manuallyName").val() != "") {
var length = storage.length;
var number = storage.length;
if (length >= number) {
number++;
var key = "Medicine" + number.toString();
var value = $("#manuallyName").val();
storage.setItem(key, value);
}
document.getElementById("manuallyName").value = "";
return true;
}
else {
alert('Please Provide Medicine Name')
return false;
}
})
});
这个脚本工作正常,它基本上根据存储大小检查存储长度并生成密钥,并将textfield
内容添加到该值中并存储它。
保存的项目然后通过另一个脚本显示在listview
与id="medList"
中。 (我不会添加脚本来节省空间,但如果需要请评论,我会添加它)。
下面的脚本是一个需要从两个listview
和localStorage
删除项目的护理:
$('ul').on('click', '.del', function (el) {
$(this).closest('li').remove();
var key = $(this).attr('key');
localStorage.removeItem(key);
$('ul.medList').listview('refresh');
});
它需要一个储存在listview
并相应删除它的“钥匙”。
现在,当一个项目从localStorage
按键变得不一致而当$('#manuallyAdd').click(function()
被再次调用,它将什么都算,如果类似的键已经存在,它将取代它(和可悲的我没有删除我的问题依赖需要它)。
让我解释,例如:
我有以下的存储:
Key = Medicine1 - Value = a
Key = Medicine2 - Value = b
Key = Medicine3 - Value = c
在上述Medicine2
的列表视图被删除,只留下Medicine1
和Medicine3
。
当我尝试添加另一个“医学”时,我期望Key = Medicine4 - Value = New Medicine
出现,但它会被存储在Medicine3
摆脱旧的价值和存储新的价值。
我相信这是由于我已经有了if statement
,而且我似乎没有找到或解决一个不同的方法,这将为我提供正确的解决方案。
我已经尝试添加一个语句,检查密钥是否已经存在并提供了一个不同的键,但这只能工作,直到另一个项目从列表中删除。
任何指针或想法将不胜感激。
对不起,但我尽力解释它。如果还有什么不清楚的地方,请告诉我。
听起来像是你需要一个对象或数组存储'Medicine'数据。 [看到这个问题](http://stackoverflow.com/questions/2010892/storing-objects-in-html5-localstorage)。 – Blazemonger