更新文本以匹配.length始终滞后。

huangapple go评论62阅读模式
英文:

Updating text to match .length is always one behind

问题

我有一些在我的网站上使用的过滤器,并且我有一个文本块,我想更新其中的活动过滤器数量,以便用户始终可以知道应用了多少个,即使过滤器已关闭。

问题是,无论哪种方式,我的计数总是落后一个。需要点击一次更多才能得到正确的数字。

我认为问题在于,数字是根据当前的数量更新的,而不是在点击发生后更新的,但我无法理解如何重新构建以解决这个问题。

以下是我的代码:

let countFilter = 0;

function updateFilterCount() {
  let countFilter = $(".is-active").length;
  $(".caps.count").text(countFilter);
  if (countFilter > 0) {
    $(".filtered-text").addClass("active");
    $(".caps.all.active").removeClass("active");
  } else {
    $(".filtered-text.active").removeClass("active");
    $(".caps.all").addClass("active");
  }
}

updateFilterCount();

$(".fltr, .fltr-label, .filter_drawer, .button.ghost.filter__button.active").on(
  "click",
  function () {
    updateFilterCount();
  }
);
英文:

I have some fitlering being used on my site and I have a text block that I would like updated with the number of active filters so the user can always tell how many are applied even if the filters are closed.

The issue is, my count is always one behind either way. It's taking one more click to get the correct number.

I would assume the number is updating with the CURRENT number as opposed to updating AFTER the click has occured, but I can't wrap my brain around how to restructure to address that.

Here's my code:

let countFilter = 0;

function updateFilterCount() {
  let countFilter = $(".is-active").length;
  $(".caps.count").text(countFilter);
  if (countFilter > 0) {
    $(".filtered-text").addClass("active");
    $(".caps.all.active").removeClass("active");
  } else {
    $(".filtered-text.active").removeClass("active");
    $(".caps.all").addClass("active");
  }
}

updateFilterCount();

$(".fltr, .fltr-label, .filter_drawer, .button.ghost.filter__button.active").on(
  "click",
  function () {
    updateFilterCount();
  }
);

答案1

得分: -2

你的点击事件处理程序似乎在你的筛选器之前执行。我建议首先使用 setTimeout() 来延迟它,或确保你的筛选器在你的计数器之前添加了 'click' 监听器。否则,你需要确切地了解处理程序的执行顺序和它们的事件。我还建议使用 toggleClass(),不需要在你的选择器中指定 .active,因为你将 active 类分配给了所有 .filtered-text.caps.all 元素。

英文:

Your click event handler seems goes before your filters.
I would try to postpone it with setTimeout() first or make sure you filters add 'click' listeners before your counter.
Otherwise you need to know exact the handler's order execution and their events.
I would also use toggleClass() and you don't need to specify .active in your selector because you assign active class to all elements of .filtered-text or .caps.all.


function updateFilterCount() {
  const countFilter = $(".is-active").length;
  $(".caps.count").text(countFilter);
  $(".filtered-text").toggleClass('active', !!countFilter);
  $(".caps.all").toggleClass("active", !countFilter);
}

updateFilterCount();

$(".fltr, .fltr-label, .filter_drawer, .button.ghost.filter__button.active").on(
  "click", () => setTimeout(updateFilterCount)
);

huangapple
  • 本文由 发表于 2023年5月18日 04:52:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定