0

我的应用程序按预期工作,直到我添加了存储在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内容添加到该值中并存储它。

保存的项目然后通过另一个脚本显示在listviewid="medList"中。 (我不会添加脚本来节省空间,但如果需要请评论,我会添加它)。

下面的脚本是一个需要从两个listviewlocalStorage删除项目的护理:

$('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的列表视图被删除,只留下Medicine1Medicine3

当我尝试添加另一个“医学”时,我期望Key = Medicine4 - Value = New Medicine出现,但它会被存储在Medicine3摆脱旧的价值和存储新的价值。

我相信这是由于我已经有了if statement,而且我似乎没有找到或解决一个不同的方法,这将为我提供正确的解决方案。

我已经尝试添加一个语句,检查密钥是否已经存在并提供了一个不同的键,但这只能工作,直到另一个项目从列表中删除。

任何指针或想法将不胜感激。

对不起,但我尽力解释它。如果还有什么不清楚的地方,请告诉我。

+0

听起来像是你需要一个对象或数组存储'Medicine'数据。 [看到这个问题](http://stackoverflow.com/questions/2010892/storing-objects-in-html5-localstorage)。 – Blazemonger

回答

1

我还没有和localStorage的工作,但这个代码块似乎很奇怪:

var length = storage.length; 
var number = storage.length; 

if (length >= number)... 

那不是总是因为storage.length = storage.length真的吗?

不管怎么说,也许你可以做这样的事情:

$(document).ready(function() { 
    if (storage.getItem("medID") === null) 
      storage.setItem("medID", "0"); 

    $('#manuallyAdd').click(function() { 
     if ($("#manuallyName").val() != "") { 

      var number = parseInt(storage.getItem("medID")); 
      number++; 
      var key = "Medicine" + number.toString(); 
      var value = $("#manuallyName").val(); 
      storage.setItem(key, value); 
      storage.setItem("medID", number.toString()); 

      document.getElementById("manuallyName").value = ""; 
      return true; 
     } 
     else { 
      alert('Please Provide Medicine Name') 
      return false; 
     } 
    }) 
}); 
+0

谢谢,这当然会使它工作,看起来更好,是的,我知道那里的“奇怪”的声明,这是毫无意义的,但它做了一段时间的工作,但现在它不得不去。我应用了你的方法,它肯定会起作用,但现在我的问题是显示localStorage中的所有项目,因此这意味着'medID'也显示出来,我真的不需要它显示。可能会想到另一种方法来在其他地方“保存”这个ID值。谢谢你! – Scur4

+1

也许你可以将值存储在JavaScript中的全局变量或页面上的隐藏字段中。如果您需要将其保存在不同的页面中,则可以将它存储在会话中或数据库中,并在需要时通过ajax进行检索。 – zgood

+0

谢谢你,你的方法和建议帮助我解决了我的问题:) – Scur4