Why can't my HTML page receive and store user input as variable data?
P粉034571623
P粉034571623 2023-09-10 16:46:27
0
1
551

Consider the code I have:

var g = document.getElementById("al").value;

function start() {
  document.getElementById("test2").innerHTML = typeof(g) + parseFloat(g);
}
<p id="test2">输出在这里:</p>
<form>
  <label for="g">a:</label>
  <input type="number" id="al" name="g" placeholder="输入a" value="55">
  <button onclick="start()" type="button">这里</button>
</form>

When I click the button, the output I get is string55, no matter what the user inputs. I want to fix it and replace the 55 with the user's actual input. So what's the fix?

P粉034571623
P粉034571623

reply all(1)
P粉633733146

You need to get the value from the function:

function start() {
  var g = document.getElementById("al").value;
  document.getElementById("test2").innerHTML = typeof(g) + parseFloat(g);
}
<p id="test2">输出在这里:</p>
<form>
  <label for="g">a:</label>
  <input type="number" id="al" name="g" placeholder="输入a" value="55">
  <button onclick="start()" type="button">这里</button>
</form>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template