0

我在这里尝试了大约 10 个其他问题的答案,但似乎没有任何效果。我已经尝试过每个地图和grep。

我想更改对象文字中的值,然后更新我的列表。

这是数组:

goals = [
  {
    goalguid: "691473a7-acad-458e-bb27-96bac224205b",
    category: "personal",
    datecreated: "2013-10-20",
    startdate: "2013-10-28",
    enddate: "2013-11-26",
    goal: "go to store",
    icon: "cart",
    status: "completed",
    username: "jtmagee",
    userguid: "0c7270bd-38e8-4db2-ae92-244de019c543"
  }, {
    goalguid: "9e693231-e6d8-4ca9-b5c8-81ea7a80a36a",
    category: "personal",
    datecreated: "2013-10-20",
    startdate: "2013-10-27",
    enddate: "2013-11-27",
    goal: "Brush Teeth",
    icon: "doctor",
    status: "inprogress",
    username: "jtmagee",
    userguid: "0c7270bd-38e8-4db2-ae92-244de019c543"
  }, {
    goalguid: "8d23005d-f6f3-4589-bb85-a90510bccc21",
    category: "work",
    datecreated: "2013-10-20",
    startdate: "2013-10-26",
    enddate: "2013-11-28",
    goal: "Arrive on Time",
    icon: "alarm",
    status: "inprogress",
    username: "jtmagee",
    userguid: "0c7270bd-38e8-4db2-ae92-244de019c543"
  }, {
    goalguid: "ac879673-19eb-43f6-8b95-078d84552da0",
    category: "school",
    datecreated: "2013-10-20",
    startdate: "2013-10-24",
    enddate: "2013-11-29",
    goal: "Do Math Homework",
    icon: "book",
    status: "missed",
    username: "jtmagee",
    userguid: "0c7270bd-38e8-4db2-ae92-244de019c543"
  }
];

我正在关闭从用户单击的 li 获得的目标引导。我使用状态键更改 li 上的类。

这是我的 jQuery,它捕获了对复选框的点击:

$(".goals").delegate("input[type=checkbox]", "click", function() {
  goalguid = $(this).parent().parent().data("goalguid");
  emailGoal = $(this).parent().data('goal');
  if ($(this).prop("checked")) {
    $(this).parent().parent().removeClass("inprogress missed").addClass("completed").prop("checked", true);
    updateStatus = 'completed';
    return updateTheGoal();
  } else {
    $(this).parent().parent().removeClass("completed").addClass("inprogress").prop("checked", false);
    updateStatus = 'inprogress';
    return updateTheGoal();
  }
});

这是我无法工作的地方。一些尝试完全清除了数组,但大多数尝试,包括这个,除了让我的列表闪烁之外什么都不做。displayGoalList 函数适用于其他用途。

updateTheGoal = function() {
  var goalList;
  $.each(goals, function() {
    if (goals.goalguid === goalguid) {
      return goals.status === updateStatus;
    }
  });
  goals = JSON.parse(localStorage["goals"]);
  goalList = $.grep(goals, function(e) {
    return e.userguid === userguid;
  });
  localStorage.setItem(userguid + "Goals", JSON.stringify(goalList));
  logSummary();
  return displayMyGoalList();
};

任何帮助深表感谢。谢谢。

4

1 回答 1

0
$.each(goals, function() {
    if (goals.goalguid === goalguid) {
      return goals.status === updateStatus;
    }
});

您正在检查goals.goalguid和设置goals.status. 这是错误的。 goals是一个数组。您的意图是检查数组中的每个元素。那么你实际上并没有对你的回报做任何事情。 returnwithineach只是中断循环或继续循环(取决于返回值)。我想你的意思是分配它。尝试这个:

$.each(goals, function(i, el) {
    if (el.goalguid === goalguid) {
        el.status = updateStatus;
        return false; // break the each
    }
});

each此外,删除此位,它将用本地存储中存储的任何内容替换您所做的任何更改:

goals = JSON.parse(localStorage["goals"]);
于 2013-11-13T22:32:44.033 回答