javascript - 如何用 javaScript 输出我的输入和答案

标签 javascript html input output

我想带 一var = input 并添加 eg: 5到该输入,然后我想将我的输入打印到我的 html 文件中。

Eg: (I know about matching getElementbyIds, I just have trouble displaying my answer and appending.

  <script>
    funtion printmsg (){
    var a = document.getElementById("myInput").value;
    var b = a+5;
     return b;
        /*i would like to keep my p[rogram going and appending  the answers until i tell the program to stop.*/
        </script>
 <body>
        <input id="myinput">
        <button onclick="printmsg">print</button>
        <span id="display"></span>

    </body>

最佳答案

您可能需要考虑使用属性onkeyup,本质上是每次您向输入字段(使用键盘)输入字符时。您分配的函数(在本例中为myFunc)将被调用。

function myFunc(elem) {
  console.log(elem.value);
  // use Number to parse number string to a number
  input = new Number(elem.value);
  output = input + 5;
  document.getElementById('out').value = output;
}
<p>input</p>
<input id="in" type="text" onkeyup="myFunc(this)"></input>
<p>answer</p>
<input id="out" type="text"></input>

关于javascript - 如何用 javaScript 输出我的输入和答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34259152/

相关文章:

javascript - 异步/等待, "unresolved variable or type await"

javascript - 无法识别我是否已单击列表项

html - 如何使用 - 显示 :Grid 制作响应式网格

php - $_Files[] 数组仅显示上传的第一个文件

string - 使用 <h :dataTable><h:inputText> on a List<String> doesn't update model values

javascript - setTimeout立即执行

javascript - 在 chrome 和 vscode 中调试 nextjs 应用程序

c# - 从网站自动下载 XLS 文件

javascript - 在其他页面上加载外部标题的内容

javascript - fine-uploader - 如何与其他输入字段结合使用?