就是这样。如何获取文本区域的旧值以便将其与新值进行比较?当前值在触发事件(例如,按键事件)后变为旧值。
我见过一些解决方案,例如,使用 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 throttle/debounce 插件的一个函数。
这正是我想要的,但是,我想在 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;
}
关于javascript - 如何获取文本区域的旧值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5215480/