Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
HTML和Javascript中的简单计算器不起作用_Javascript - Fatal编程技术网

HTML和Javascript中的简单计算器不起作用

HTML和Javascript中的简单计算器不起作用,javascript,Javascript,我有这个HTML(只是一个简单的袖珍计算器): 显示按键(数字和数学符号),警报工作正常,但不显示支出评估。我错在哪里 您应该使用getElementById而不是ByVal,这不是一个函数。 var s=''; 函数getValue(val){ 如果(val!='C'&&val!='='){s+=val; document.getElementById(“dsp”).value+=val;} 如果(val=='C') document.getElementById(“dsp”).value=“

我有这个HTML(只是一个简单的袖珍计算器):


显示按键(数字和数学符号),警报工作正常,但不显示支出评估。我错在哪里

您应该使用
getElementById
而不是
ByVal
,这不是一个函数。
var s='';
函数getValue(val){
如果(val!='C'&&val!='='){s+=val;
document.getElementById(“dsp”).value+=val;}
如果(val=='C')
document.getElementById(“dsp”).value=“”;
如果(val=='='='){
警报(document.getElementById(“dsp”).value);
var res=评估值;
document.getElementById(“dsp”).value=res;
}
}
简单计算器


将最后一行更改为使用getElementById,而不是getElementByVal

为什么要重新发明轮子。。。?
 <html>
  <head>
    <script type="text/javascript" src="js/SimpleCalculator.js"></script>
 </head>
 <body>
    <h2>Simple Calculator</h2>
    <div class="calculator">
        <div class="display"><input type="text" readonly size="20" id="dsp"></div>
        <div class="keys">
            <p><input type="button" value="7" onclick='getValue(this.value)'>
               <input type="button" value="8" onclick='getValue(this.value)'>
               <input type="button" value="9" onclick='getValue(this.value)'>
               <input type="button" value="/" onclick='getValue(this.value)'>
            </p>
            <p><input type="button" value="4" onclick='getValue(this.value)'>
                <input type="button" value="5" onclick='getValue(this.value)'>
                <input type="button" value="6" onclick='getValue(this.value)'>
                <input type="button" value="*" onclick='getValue(this.value)'>
            </p>
            <p><input type="button" value="1" onclick='getValue(this.value)'>
               <input type="button" value="2" onclick='getValue(this.value)'>
               <input type="button" value="3" onclick='getValue(this.value)'>
               <input type="button" value="-" onclick='getValue(this.value)'>
            </p>
            <p><input type="button" value="0" onclick='getValue(this.value)'>
               <input type="button" value="." onclick='getValue(this.value)'>
               <input type="button" value="+" onclick='getValue(this.value)'></p>
            <p><input type="button" value="C" onclick='getValue(this.value)'>
               <input type="button" value="=" onclick='getValue(this.value)'></p>
        </div>
    </div>
</body>
</html>
var s = '';

function getValue(val){
alert(val);

if (val != 'C' && val != '='){s += val;
    document.getElementById("dsp").value+=val;}
if (val === 'C')
    document.getElementById("dsp").value = " ";
if (val === '='){
    alert(document.getElementByVal("dsp").value);
    var res = eval(s);
    document.getElementByVal("dsp").value = res;
}
}