【问题标题】:delete button its deleting all the values删除按钮它删除所有值
【发布时间】:2016-07-15 06:22:45
【问题描述】:

当我单击删除按钮时,它会删除所有值。是否可以删除每个值?

我提供以下代码:

http://codepen.io/anon/pen/grRKEz?editors=1010

$('#localStorageTest').submit(function(e) {
    e.preventDefault();
    var email = $('#email').val();
    var name = $('#name').val();
    var message = $('#message').val();
    var div = "<div><span>"+name+"</span><span >"+email+"</span><span>"+message+"</span><input type='button' value='Edit' name='editHistory'><input type='button' value='Delete' name='deleteHistory'></div>";  //add your data in span, p, input.. 
    //alert(div);

    $('.gettingValues').append(div); //apendd the div
    $('#localStorageTest')[0].reset(); //clear the form 
    localStorage.clear();
 });

【问题讨论】:

  • 您的删除按钮正在将全新的 HTML 插入到您的“gettingValues”div 中。这具有覆盖 div 中所有现有 html 的效果,而不仅仅是覆盖您尝试删除的行。
  • @Brian 你能在我的codepen中更新它令人困惑的地方吗:(
  • 我还是不太确定你想让删除按钮完成的意图是什么
  • @Brian 我需要为每个项目实现删除功能

标签: javascript jquery css html


【解决方案1】:

由于表单将包含在 div 中的信息附加到您的“gettingValues”div 中,因此您可以删除父元素。

像这样:

$('.gettingValues').on('click', "input[name='deleteHistory']", function() {
  //var div = "getting form values<input data-name='edit' type='button' value='Edit' name='editHistory'><input data-name='delete' type='button' name='deleteHistory' value='Delete'>"; //No need for this line.
  console.log("Data deleted");
  //$('.gettingValues').html(div); //Don't reset the html.
  if($(this).parent().hasClass('gettingValues')) return false; //Don't remove the container.
  $(this).parent().remove(); //Remove the containing div only.
  //deleteHistoryAPI(data);

});

查看:http://codepen.io/gborbonus/pen/grRjjy?editors=1011

【讨论】:

  • 希望他不会要求您也将其从 localStorage 中删除 :)
  • 这是正确的答案 texi
  • @miro 你知道如何在 localStorage 中删除以便我学习
  • 您仅在 keyup 事件时存储在 LocalStorage 中(仅在填写表单时)。执行时:localStorage.clear();在您的提交功能中,您正在清除本地存储。
猜你喜欢
  • 2013-03-12
  • 2023-01-22
  • 1970-01-01
  • 2023-03-24
  • 2015-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多