簡體   English   中英

如何獲取textarea的舊值

[英]How to get the old value of a textarea

就是這樣。 如何獲取 textarea 的舊值以便將其與新值進行比較? 觸發事件(例如,keyup 事件)后,當前值變為舊值。

我已經看到了一些解決方案,例如,使用 cookie 來記錄舊值和新值,但是,由於我想稍后執行的操作類型,這種解決方案在我的情況下不起作用。

希望有一個比這更好的建議。

更新

在遵循@drachenstern、@Matthew、@Peter 的一些建議之后,我得到了這樣的結果

var startTimer = null;
var oldValue;
$("#textarea").keydown($.debounce( 1000, true, function(){
oldValue = $("#textarea").val();
}
));

$("#textarea").keyup(function(){
if(startTimer) clearTimeout(startTimer);
startTimer = setTimeout(function(){
var newValue = $("#textarea").val();
d = // here a clever comparison
oldValue = newValue;
},2000);
})

順便說一下, $.debounce是來自 jQuery 節流/去抖動插件的一個函數。

這正是我想要的,但是,我想在 setTimeout 函數和 keyup 函數之外獲取變量d ,以便在其他地方使用它。 然而,clearTimeout 在返回值方面似乎很棘手。 有什么辦法可以得到d的值嗎?

var oldValue = document.getElementById("textareaid").value;

document.getElementById("textareaid").onchange = function(){
  var newValue = this.value;
  //Compare oldValue and newValue
  oldValue = newValue;
}

暫無
暫無

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

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