gpt4 book ai didi

javascript - 为什么 .change() 不起作用?

转载 作者:行者123 更新时间:2023-11-28 18:44:19 25 4
gpt4 key购买 nike

我有html

<span class="offersReceivedCount">3</span>

我想在 3 值更改为其他值时运行代码。我使用的是 Handlebars ,所以真正的 html 看起来像

<span class="offersReceivedCount">{{offers}}</span> Offers Received</p>

我试过了

$(".offersReceivedCount").change(function(){
console.log("change");
});

但是当 {{offers}} 的值发生变化时,它不会记录任何内容

我需要不同的事件类型或者尝试不同的方法吗?

最佳答案

您需要的是一个ReactiveVar并定义一个equality function关于这一点。

Template['your-template'].onCreated = function() {
this['offerReact'] = new ReactiveVar(this['offers'], function(oldValue, newValue) {
if (oldValue !== newValue) {
// run the function you want to trigger when value change
return false;
} else {
// no change, do nothing, just return true;
return true;
}

});
}

因此,每当您为 offerReact 设置新值时,equal 函数就会被触发,然后开始运行您想要运行的事件

关于javascript - 为什么 .change() 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35641055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com