[英]how to input numbers using buttons in to a textbox
I want to input numbers using buttons into the textbox how to do that?我想使用按钮在文本框中输入数字怎么做?
I have a problem on displaying buttons to textbox, I can only store one function in one button for example <button onclick="getNumber(1)">1</button
>我在向文本框显示按钮时遇到问题,我只能在一个按钮中存储一个功能,例如<button onclick="getNumber(1)">1</button
>
function calc() { var a, b, c; a = Number(document.getElementById('num1').value); b = Number(document.getElementById('num2').value); c = document.getElementById('operators').value; if (c === '+') { x = document.getElementById('ans').value = (a + b); } if (c === '-') { x = document.getElementById('ans').value = (a - b); } if (c === '*') { x = document.getElementById('ans').value = (a * b); } if (c === '/') { x = document.getElementById('ans').value = (a / b); } }
<input type="text" id="num1"><br> <input type="text" id="num2"><br> <select id="operators"> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </select> <br> <button onclick="calc()">=</button><br> <input type="text" id="ans">
function calc() { var a, b, c, x = ""; a = Number(document.getElementById('num1').value); b = Number(document.getElementById('num2').value); c = document.getElementById('operators').value; if (c === '+') { x = (a + b); } else if (c === '-') { x = (a - b); } else if (c === '*') { x = (a * b); } else if (c === '/') { x = (a / b); } document.getElementById('ans').value = x; } window.addEventListener("load", function() { // when the page loads document.getElementById("calc").addEventListener("click", calc); document.getElementById("buts").addEventListener("click", function(e) { var tgt = e.target; if (tgt.classList.contains("but")) { var val = tgt.value; console.log(val); // not sure what you want to do here } }); });
<input type="text" id="num1"><br> <input type="text" id="num2"><br> <select id="operators"> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </select> <br> <div id="buts"> <button type="button" class="but" value="1">1</button> <button type="button" class="but" value="2">2</button> <button type="button" class="but" value="3">3</button> </div> <button type="button" id="calc">=</button><br> <input type="text" id="ans">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.