Javascript计算器记忆功能

时间:2014-02-23 23:50:36

标签: javascript html

这是我的HTML

    <script type="text/javascript" src="./The Desktop Calculator_files/calc.js"></script>
    <style type="text/css"></style>
</head>

<body onLoad="checkBrowser()">
<form id="calcForm">

<div id="calc">
<input type='hidden' id='param1' value='0' />
<input type='hidden' id='operator' value='' />
    <div id="display">
        <input type="text" name="disp" id="disp" class="disp" size="36" value="0">
    </div>
    <div id="buttons">
        <div class="row">
            <input type="button" value="7" onclick="isNum();appendMe(this.value)">
            <input type="button" value="8" onclick="isNum();appendMe(this.value)">
            <input type="button" value="9" onclick="isNum();appendMe(this.value)">
            <input type="button" value="/" onClick="isNum();setOp(this.value)">
            <input type="button" value="CE">
        </div>
        <div class="row">
            <input type="button" value="4" onclick="isNum();appendMe(this.value)">
            <input type="button" value="5" onclick="isNum();appendMe(this.value)">
            <input type="button" value="6" onclick="isNum();appendMe(this.value)">
            <input type="button" value="*" onClick="isNum();setOp(this.value)">
            <input type="button" value="C" onclick="clearAll()">
        </div>
        <div class="row">
            <input type="button" value="1" onclick="isNum();appendMe(this.value)">
            <input type="button" value="2" onclick="isNum();appendMe(this.value)">
            <input type="button" value="3" onclick="isNum();appendMe(this.value)">
            <input type="button" value="-" onClick="isNum();setOp(this.value)">
            <input type="button" value="M" onClick="isNum();set_getMem()">
        </div>
        <div class="row">
            <input type="button" value="0" onclick="isNum();appendMe(this.value)">
            <input type="button" value="+/-" onclick="isNum();plusMinus()">
            <input type="button" value="." onclick="isNum();appendMe(this.value)">
            <input type="button" value="+" onClick="isNum();setOp(this.value)">
            <input type="button" value="=" onClick="isNum();calcMe()">
        </div>
        </div>      
<div id='warning'>Your Browser Can't Handle The Truth!</div> 
</div>
</form>


</body></html>

这是我的JavaScript

function appendMe(val)
{
    //alert(val);
    //document.getElementById("disp").value+=val;
    //alert(val);
if(document.getElementById("disp").value=='0')
{
  document.getElementById("disp").value=val;
}
else if(val=='.' && document.getElementById("disp").value.indexOf('.')>-1) //do nothing, because we already have a decimal point
{

}
else //in any other case, we just append
{
  document.getElementById("disp").value+=val;
}
    }
function clearAll()
{
    //alert(val);
    document.getElementById("disp").value=0;
}

function checkBrowser()
{
    alert("checking");
    document.getElementById("warning").style.display="none";
}

function plusMinus()
{
    document.getElementById("disp").value=(document.getElementById("disp").value*-1);
}
function setOp(val)
{
//first, set aside the initial value as entered
document.getElementById("param1").value=document.getElementById("disp").value;

//next, clear out that first number entered
document.getElementById("disp").value=0;

//finally, store the operation
document.getElementById("operator").value=val;
} 
function calcMe()
{
var param1 = document.getElementById("param1").value;
var operator = document.getElementById("operator").value;
var param2 = document.getElementById("disp").value;

document.getElementById("disp").value = eval(param1+operator+param2); 
}

function isNum()
{
//start as true
var isN = true;

if(isNaN(document.getElementById("disp").value))
{
isN=false;
alert("Non-numeric Data!");
}

return isN;
}


function set_getMem()
{
var memvalue;
//{
//isNum()
//}
if(memvalue == null ) //nothing in there, so set it
    {
    memvalue = document.getElementById("disp").value;
    }

else //something in there, so display it
    {
    document.getElementById("disp").value = memvalue;
    }
}

我遇到问题的部分是让M按钮正常工作。我想要发生的是,我可以点击M,它将保存显示中的任何内容,除非已经存储了一个数字,我希望它显示该数字。 目前我单击M按钮,它似乎不会保存数字或显示数字。

编辑:基于反馈,我得到了Memory功能,但现在我需要一个可以清除全局变量值的函数。

function clear_All()
{
var memvalue=0;
document.getElementById("disp").value=0;
var param1=0;
var param2=0;

}

当我在该函数中将memvalue置于0时,它不会从memvalue中清除它。当我把它放在函数之外时它只会破坏memvalue的存储能力。

1 个答案:

答案 0 :(得分:1)

这可能是问题所在:

function set_getMem()
{
  var memvalue;

您将memvalue定义为set_memGet()内的局部变量,因此一旦函数返回,此变量就会消失。

从函数中定义此变量。