我将如何完成此脚本,该脚本将用户输入乘以 100,然后在浏览器中将其输出到控制台

How would I finish this script that multiply user input by 100 and then outputs it to the console when in a browser?

本文关键字:脚本 浏览器 然后 输出 控制台 何完成 用户 输入      更新时间:2023-09-26

我正在尝试做的是创建一个脚本,该脚本可以获取用户输入到字段中的任何数字并将其乘以 100,并在按下按钮时将其输出到控制台。我已经部分完成了它,但我不熟悉控制台.log并且总体上是javascript的新手。我一直在四处阅读,但我似乎无法弄清楚似乎应该如此简单的事情。它只有 1 个字段 + 1 个按钮。

这是我的 HTML:

<html>
    <head lang="en">
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <h1>Java Help</h1>
        <input id="Variables"/>
        <input type="button" onclick="getInput()"/>
        <script src="js/main.js"></script>
    </body>
</html>

我的Javascript在这里:

/**
* Created by Jabrian on 1/24/2015.
*/
function getInput(){
var userInput= document.getElementByID("Variables").value;
console.log("100" * userInput);

它应该是getElementById而不是getElementByID .见下文:

function getInput() {
  var userInput= document.getElementById("Variables").value;
  var answer = 100 * parseFloat(userInput);
  console.log(answer);
  alert(answer);
}
<input id="Variables" value="10"/>
<input type="button" value="Multiply" onclick="getInput()"/>