簡體   English   中英

為什么.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}}的值更改時記錄任何內容

我需要其他事件類型還是嘗試其他方法?

您需要一個ReactiveVar並在其上定義一個相等函數

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函數,然后開始運行要運行的事件

另一種方法(我不知道它對您的效果如何)是使輸入看起來像span標記,請參見小提琴: 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM