我怎样才能让变量保留旧信息? [英] How can I make a variable retain old information?
问题描述
< textarea id =mytext>< / textarea>
< input onclick =MyFunction(); type =submitvalue =submit/>
函数MyFunction(){
var MyVariable = document.getElementById(mytext)。value();
var MyVariable2 = document.getElementById(mytext)。value();
if(MyVariable === MyVariable2){
alert('text is the same');
}
else
{
alert('text is different');
}
}
http://jsfiddle.net/2eFD2/4/
如何让变量保留旧信息?
通过不分配新信息它。
只需在函数外部创建一个变量,以便它在函数调用之间持续存在,并且只在它没有时才分配给它一个值:
var previousValue = null;
函数MyFunction(){
var currentValue = document.getElementById(mytext).value;
if(previousValue === currentValue){
alert('text is the same');
}
else
{
alert('text is different');
}
if(previousValue == null){
//只在第一次调用函数时执行
previousValue = currentValue;
$ / code $ / pre
lockquote
我需要变量与旧的信息落后第二个变量与信息第二个变量之前被给予新的信息。
然后你只是总是分配变量的新值:
var previousValue = null;
函数MyFunction(){
var currentValue = document.getElementById(mytext).value;
if(previousValue === currentValue){
alert('text is the same');
}
else
{
alert('text is different');
}
previousValue = currentValue;
}
So for example, let's say I define a variable as being equivalent to some text typed into a textarea then I change the text in the textarea to a new value. I want to be able to keep the old value stored in the variable originally and compare it with the new value stored. I thought maybe I could do this with two variables. What I want to do with the code is find out whether or not the value I entered originally is the same as the new value I enter or not. Here's an attempt. I don't expect the code below to work at all. It's only meant to give an idea of what I want to do.
<textarea id="mytext"></textarea>
<input onclick="MyFunction();" type="submit" value="submit"/>
function MyFunction(){
var MyVariable = document.getElementById("mytext").value();
var MyVariable2 = document.getElementById("mytext").value();
if(MyVariable === MyVariable2){
alert('the text is the same');
}
else
{
alert('the text is different');
}
}
解决方案
How can I make a variable retain old information?
By not assigning new information to it.
Simply create a variable outside of the function, so that it persists between function calls, and only assign to it if it doesn't have a value yet:
var previousValue = null;
function MyFunction(){
var currentValue = document.getElementById("mytext").value;
if (previousValue === currentValue){
alert('the text is the same');
}
else
{
alert('the text is different');
}
if (previousValue == null) {
// only executed the first time the function is called
previousValue = currentValue;
}
}
I need the variable with the old information to lag behind the second variable with information the second variable had before being given new info.
Then you just always assign the new value to the variable:
var previousValue = null;
function MyFunction(){
var currentValue = document.getElementById("mytext").value;
if(previousValue === currentValue){
alert('the text is the same');
}
else
{
alert('the text is different');
}
previousValue = currentValue;
}
这篇关于我怎样才能让变量保留旧信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!