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

Why doesn't .change() work?

我有 html

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

并且我想在 3 值更改为其他值时 运行 编码。我正在使用把手,所以真正的 html 看起来像

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

我试过了

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

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

我需要不同的事件类型还是尝试不同的方法?

一种不同的方法(我不知道它对你的效果如何)是像 span 标签一样输入 "look",参见 fiddle:https://jsfiddle.net/f1a990f1/

那么您的代码将起作用,因为更改函数不适用于 span 标记。

HTML

<input class="offersReceivedCount" type="text" value="333">
<script>
  $(".offersReceivedCount").change(function() {
    alert("change");
  });

</script>

CSS

input {
  border: none;
  outline: none;
  background-color: transparent;
  font-family: inherit;
  font-size: inherit;
}

您需要的是 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函数就会被触发,然后开始运行你想要的事件运行