Button 将按钮的值打印到文本中

Button 将按钮的值打印到文本中,button,text,insert,Button,Text,Insert,嗯…我想在点击时将按钮的值插入文本输入,但是js返回“未定义”按钮。看起来我想制作计算器,是的,但是制作计算器的第一步是在文本中插入按钮的值 <html> <head> <meta charset="utf-8"/> <title>My Calculator</title> <style type="text/css"> </style> <script type=

嗯…我想在点击时将按钮的值插入文本输入,但是js返回“未定义”按钮。看起来我想制作计算器,是的,但是制作计算器的第一步是在文本中插入按钮的值

<html>
<head>
    <meta charset="utf-8"/>
    <title>My Calculator</title>
    <style type="text/css">

    </style>
    <script type="text/javascript">
        function action(){
            /* This is for testing the function to run
            alert('Successful!');*/
            var val = document.getElementsByName('num').value;
            document.getElementById(/*'result' or 'answer'*/).innerHTML= val;
        }
    </script>
</head>
<body>
    <input id="result" type="text" readonly /><br/>
    <button name="num" value="1" onclick="action()">1</button>
    <button name="num" value="2" onclick="action()">2</button>
    <button name="num" value="3" onclick="action()">3</button>
    <button name="operand">+</button><br/>
    <button name="num" value="4" onclick="action()">4</button>
    <button name="num" value="5" onclick="action()">5</button>
    <button name="num" value="6" onclick="action()">6</button>
    <button name="operand">-</button><br/>
    <button name="num" value="7" onclick="action()">7</button>
    <button name="num" value="8" onclick="action()">8</button>
    <button name="num" value="9" onclick="action()">9</button>
    <button name="operand">*</button><br/>
    <button name="operand">C</button>
    <button name="num" value="0" onclick="action()">0</button>
    <button name="operand">=</button>
    <button name="operand">/</button>
    <h1 id="answer"></h1>
</body>

我的计算器
函数动作(){
/*这用于测试要运行的函数
警报(“成功!”)*/
var val=document.getElementsByName('num').value;
document.getElementById(/*'result'或'answer'*/).innerHTML=val;
}

1. 2. 3. +
4. 5. 6. -
7. 8. 9 *
C 0 = /

你能这样试试吗

html


此外,您对多个元素使用相同的名称

是。我试过了,但还没有解决。你可以给我发一个完整的代码也许我的问题解决了
<button name="num" id="9" value="9" onclick="action(9)">9</button>
    function action(id){
      /* This is for testing the function to run
      alert('id!');*/

      var val = document.getElementById(id).value;

      document.getElementById('result').value += val;
    }