通过按钮将值插入输入标签

Inserting values into input tag via buttons

本文关键字:插入 输入 标签 按钮      更新时间:2023-09-26

我正在制作一个计算器只是为了学习Javascript(我非常了解HTML和CSS(,并且我有计算器的基础。我想让你只使用按钮。按钮工作和所有。但是例如,如果我想插入 11,那是不可能的,因为我只能插入个位数,我不知道为什么:(这是您按下以插入数字 1 的按钮。

<button type="button" onclick="no1 ()">1</button>

这是将数字 1 插入输入标签的 JavaScript:

var currentinput= document.getElementById("firstnumber");
var answer= document.getElementById("answerswer");
function no1 ()
{
currentinput.value=1;
answer.value=1;
}

我怎么能插入 11 而不是 1

如果你想在变量的末尾添加一些东西,你可以使用这样的东西:

function no1 ()
{
    currentinput.value = currentinput.value+"1";
    answer.value=1;
}

这将采用 currentinput.value 中已有的数字,并在末尾加 1。