我怎样才能让变量保留旧信息? [英] How can I make a variable retain old information?

查看:126
本文介绍了我怎样才能让变量保留旧信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,假设我将一个变量定义为等同于输入到textarea中的某些文本,然后将textarea中的文本更改为新值。我希望能够保留最初存储在变量中的旧值并将其与存储的新值进行比较。我想也许我可以用两个变量来做到这一点。我想要对代码进行的操作是查明我最初输入的值是否与我输入的新值相同。这是一个尝试。我不希望下面的代码可以工作。这只是为了说明我想要做什么。

 < 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');
    }
}

http://jsfiddle.net/2eFD2/4/

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆