Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.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
Javascript 函数b()中的错误_Javascript_Html_Calculator - Fatal编程技术网

Javascript 函数b()中的错误

Javascript 函数b()中的错误,javascript,html,calculator,Javascript,Html,Calculator,我试图用javascript制作一个简单的计算器。然而,代码似乎不起作用。函数a、clr正常工作,而函数b和res不工作。有人能帮我做同样的事吗 <html> <head> <title></title> <script> var num1, cal; function a(x) { document.f.num.value += x; }

我试图用javascript制作一个简单的计算器。然而,代码似乎不起作用。函数a、clr正常工作,而函数b和res不工作。有人能帮我做同样的事吗

<html>
<head>
    <title></title>
    <script>
        var num1, cal;
        function a(x) {
            document.f.num.value += x;
        }
        function b(x) {
            num1 = parseInt(document.f.num.value);
            document.f.num.value = "";
            cal = x;
        }
        function clr() {
            num1 = null;
            cal = null;
            document.f.num.value = "";
        }
        function res() {
            switch (cal) {
                case '+':
                    document.f.num.value = num1 + parseInt(document.f.num.value);
                    break;
                case '-':
                    document.f.num.value = num1 - parseInt(document.f.num.value);
                    break;
                case '*':
                    document.f.num.value = num1 * parseInt(document.f.num.value);
                    break;
                case '/':
                    document.f.num.value = num1 / parseInt(document.f.num.value);
                    break;
            }
        }
    </script>
    <style>
        input[type="button"] {
            width:35px;
        }
    </style>
</head>
<body>
    <form name="f">
        <input type="text" name="num" value="">
        <br>
        <input type="button" name="7" value="7" onclick="a(7)">
        <input type="button" name="8" value="8" onclick="a(8)">
        <input type="button" name="9" value="9" onclick="a(9)">
        <input type="button" name="+" value="+" onclick="b(+)">
        <br>
        <input type="button" name="4" value="4" onclick="a(4)">
        <input type="button" name="5" value="5" onclick="a(5)">
        <input type="button" name="6" value="6" onclick="a(6)">
        <input type="button" name="-" value="-" onclick="b(-)">
        <br>
        <input type="button" name="1" value="1" onclick="a(1)">
        <input type="button" name="2" value="2" onclick="a(2)">
        <input type="button" name="3" value="3" onclick="a(3)">
        <input type="button" name="*" value="*" onclick="b(*)">
        <br>
        <input type="button" name="0" value="0" onclick="a(0)">
        <input type="button" name="C" value="C" onclick="clr(C)">
        <input type="button" name="=" value="=" onclick="res(=)">
        <input type="button" name="/" value="/" onclick="b(/)">
        <br>
    </form>
</body>

您需要以字符串形式传递操作,例如:

<input type="button" name="-" value="-" onclick="b('-')">

i、 e.onclick处理程序变为b'-'而不是b-

定义不工作请检查JavaScript控制台并在此处发布错误消息(如果有)。如果你不知道JavaScript控制台是什么,你可以忽略我的评论。res功能仍不工作:你有没有把=也加上引号?