如何获得文本区域的旧值 [英] How to get the old value of a textarea
问题描述
就是这样.如何获得文本区域的旧值以便将其与新值进行比较?触发事件(例如,键入事件)后,当前值将变为旧值.
That's it. How can I get the old value of a textarea in order to compare it with a new value?. The current value becomes the old value after triggering an event (say, a keyup event).
我看到了一些解决方案,例如,使用cookie记录旧值和新值,但是,由于我要稍后执行的操作类型,这种解决方案在我的情况下不起作用.
I have seen some solutions, for example, using cookies to record the old value and the new value, however, such solution doesn't work in my case because of the type of manipulation I want to perform later.
希望有一个比这更好的建议.
Hopefully, there is a suggestion which works better than that.
更新:
在遵循@ drachenstern,@ Matthew,@ Peter的一些建议之后,我最终得到了这样的东西
After following some suggestions from @drachenstern, @Matthew, @Peter, I ended up with something like this
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节流/去抖动插件中的函数.
By the way, $.debounce
is a function from the jQuery throttle / debounce plugin.
这正是我想要的,但是,我想在setTimeout函数和keyup函数之外获取变量 d
,以便在其他地方使用它.但是,当涉及到返回值时,clearTimeout似乎很棘手.以任何方式获取 d
的值吗?.
This does exactly what I want, however, I'd like to obtain the variable d
outside of setTimeout function and of keyup function, in order to use it elsewhere. However, clearTimeout seems to be tricky when it comes to return values. Any way to get the value of the d
?.
推荐答案
var oldValue = document.getElementById("textareaid").value;
document.getElementById("textareaid").onchange = function(){
var newValue = this.value;
//Compare oldValue and newValue
oldValue = newValue;
}
这篇关于如何获得文本区域的旧值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!