为什么我的HTML网页中用户输入的内容没有被接收并存储为变量数据?

huangapple go评论71阅读模式
英文:

Why is not my user input in HTML webpage taken and stored as a variable data?

问题

The code you provided is in HTML and JavaScript. To fix the issue and replace "55" with the user's actual input, you can update the JavaScript code as follows:

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

function start() {
  var userInput = parseFloat(g);
  document.getElementById("test2").innerHTML = "Output is here: " + userInput;
}

With this change, the value entered by the user in the input field will be displayed as part of the output message when the button is clicked.

英文:

Consider the code I have:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var g = document.getElementById(&quot;al&quot;).value;

function start() {
  document.getElementById(&quot;test2&quot;).innerHTML = typeof(g) + parseFloat(g);
}

<!-- language: lang-html -->

&lt;p id=&quot;test2&quot;&gt;Output is here:&lt;/p&gt;
&lt;form&gt;
  &lt;label for=&quot;g&quot;&gt;a:&lt;/label&gt;
  &lt;input type=&quot;number&quot; id=&quot;al&quot; name=&quot;g&quot; placeholder=&quot;Enter a&quot; value=&quot;55&quot;&gt;
  &lt;button onclick=&quot;start()&quot; type=&quot;button&quot;&gt;Here&lt;/button&gt;
&lt;/form&gt;

<!-- end snippet -->

The output I get on clicking the button is string55, whatever the user input is. I want to fix it and replace 55 with user's actual input. What is the fix then?

答案1

得分: 1

Sure, here are the translated parts:

你需要从函数内部获取值:

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>

请注意,使用内联的HTML on* 处理程序被认为是不良的编程习惯。它难以调试。JavaScript 应该只出现在一个地方,而不是使用 Element.addEventListener()

const elBtn = document.querySelector("#start");
const elTest = document.querySelector("#test2");
const elG = document.querySelector("[name=g]");

const printGValue = () => {
  const g = elG.value;
  elTest.textContent = typeof(g) + parseFloat(g);
};

// 点击时执行
elBtn.addEventListener("click", printGValue);
// 初始化时执行:
printGValue();
<form>
  <label>
    a: <input type="number" name="g" placeholder="输入a" value="55">
  </label>
  <button id="start" type="button">这里</button>
</form>

<p id="test2"></p>
英文:

You need to grab the value from within the function:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function start() {
  var g = document.getElementById(&quot;al&quot;).value;
  document.getElementById(&quot;test2&quot;).innerHTML = typeof(g) + parseFloat(g);
}

<!-- language: lang-html -->

&lt;p id=&quot;test2&quot;&gt;Output is here:&lt;/p&gt;
&lt;form&gt;
  &lt;label for=&quot;g&quot;&gt;a:&lt;/label&gt;
  &lt;input type=&quot;number&quot; id=&quot;al&quot; name=&quot;g&quot; placeholder=&quot;Enter a&quot; value=&quot;55&quot;&gt;
  &lt;button onclick=&quot;start()&quot; type=&quot;button&quot;&gt;Here&lt;/button&gt;
&lt;/form&gt;

<!-- end snippet -->

PS, using inline HTML on* handlers is considered bad programming habit. It's hard to debug. JS should be in one place only, instead, use Element.addEventListener()

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const elBtn = document.querySelector(&quot;#start&quot;);
const elTest = document.querySelector(&quot;#test2&quot;);
const elG = document.querySelector(&quot;[name=g]&quot;);

const printGValue = () =&gt; {
  const g = elG.value;
  elTest.textContent = typeof(g) + parseFloat(g);
};

// Do on click
elBtn.addEventListener(&quot;click&quot;, printGValue);
// And on init:
printGValue();

<!-- language: lang-html -->

&lt;form&gt;
  &lt;label&gt;
    a: &lt;input type=&quot;number&quot; name=&quot;g&quot; placeholder=&quot;Enter a&quot; value=&quot;55&quot;&gt;
  &lt;/label&gt;
  &lt;button id=&quot;start&quot; type=&quot;button&quot;&gt;Here&lt;/button&gt;
&lt;/form&gt;

&lt;p id=&quot;test2&quot;&gt;&lt;/p&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月5日 01:25:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76614772.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定