简体   繁体   English

使用 onkeyup 在打字时显示 output

[英]Using onkeyup to display output whilst typing

I am trying to use "onkeyup" to display the realtime output of a form.我正在尝试使用“onkeyup”来显示表单的实时 output。 It appears to be working just fine in my Codepen project, but not in VS Code.它似乎在我的 Codepen 项目中工作得很好,但在 VS Code 中却不行。 I can't pinpoint where I'm going wrong.我无法确定我哪里出错了。

Here's my Codepen: https://codepen.io/jonah-cockshaw/pen/OJOrapb这是我的代码笔: https://codepen.io/jonah-cockshaw/pen/OJOrapb

Here's the code from VS Code:这是来自 VS Code 的代码:

<h3>Chiller capacity</h3>
<p>Please input each chiller’s rated capacity in kW. The rated capacity is on the chiller nameplate.</p>

<label for="chiller-1">Chiller 1</label><br>
<input type="number" onkeyup=`addAll()` class="input-field" id="chiller-1" name="chiller-1" placeholder="Input number in KW" value="" required><br>

<label for="chiller-2">Chiller 2</label><br>
<input type="number" onkeyup=`addAll()` class="input-field" id="chiller-2" name="chiller-2"placeholder="Input number in KW" value=""><br>

<label for="chiller-3">Chiller 3</label><br>
<input type="number" onkeyup=`addAll()` class="input-field" id="chiller-3" name="chiller-3" placeholder="Input number in KW" value=""><br>

<label for="chiller-4">Chiller 4</label><br>
<input type="number" onkeyup=`addAll()` class="input-field" id="chiller-4" name="chiller-4" placeholder="Input number in KW" value=""><br>

<label for="chiller-5">Chiller 5</label><br>
<input type="number" onkeyup=`addAll()` class="input-field" id="chiller-5" name="chiller-5" placeholder="Input number in KW" value=""><br>

<p id="output">Output goes here</p>

function addAll() {
const chiller1 = Math.floor(document.getElementById("chiller-1").value);
const chiller2 = Math.floor(document.getElementById("chiller-2").value);
const chiller3 = Math.floor(document.getElementById("chiller-3").value);
const chiller4 = Math.floor(document.getElementById("chiller-4").value);
const chiller5 = Math.floor(document.getElementById("chiller-5").value);
const allChillersValue = chiller1 + chiller2 + chiller3 + chiller4 + chiller5;
// console.log(allChillersValue);

document.getElementById('output').innerHTML = allChillersValue;
};

the issue come from the way you add the keyup event listener问题来自您添加 keyup 事件侦听器的方式

 onkeyup=`addAll()`

the valid html syntax is有效的 html 语法是

onkeyup="addAll()"

but a better way i can advice is use addEventlistener method on all input-field class但我建议的更好方法是在所有输入字段 class 上使用 addEventlistener 方法

[...document.getElementsByClassName('input-field')].forEach(input => {
  input.addEventListener('keyup', addAll);
});

 [...document.getElementsByClassName('input-field')].forEach(input => { input.addEventListener('keyup', addAll); }); function addAll() { const chiller1 = Math.floor(document.getElementById("chiller-1").value); const chiller2 = Math.floor(document.getElementById("chiller-2").value); const chiller3 = Math.floor(document.getElementById("chiller-3").value); const chiller4 = Math.floor(document.getElementById("chiller-4").value); const chiller5 = Math.floor(document.getElementById("chiller-5").value); const allChillersValue = chiller1 + chiller2 + chiller3 + chiller4 + chiller5; console.log(allChillersValue); document.getElementById('output').innerHTML = allChillersValue; }
 <h3>Chiller capacity</h3> <p>Please input each chiller's rated capacity in kW. The rated capacity is on the chiller nameplate.</p> <label for="chiller-1">Chiller 1</label><br> <input type="number" class="input-field" id="chiller-1" name="chiller-1" placeholder="Input number in KW" value="" required><br> <label for="chiller-2">Chiller 2</label><br> <input type="number" class="input-field" id="chiller-2" name="chiller-2"placeholder="Input number in KW" value=""><br> <label for="chiller-3">Chiller 3</label><br> <input type="number" class="input-field" id="chiller-3" name="chiller-3" placeholder="Input number in KW" value=""><br> <label for="chiller-4">Chiller 4</label><br> <input type="number" class="input-field" id="chiller-4" name="chiller-4" placeholder="Input number in KW" value=""><br> <label for="chiller-5">Chiller 5</label><br> <input type="number" class="input-field" id="chiller-5" name="chiller-5" placeholder="Input number in KW" value=""><br> <p id="output">Output goes here</p>

It's a copy-paste issue.这是一个复制粘贴的问题。 VS Code has turned single quotes ' into ticks ` . VS Code 已将单引号'变成刻度线`

Change the ticks surrounding the onkeyup property value for each element to a single or double quote instead.将每个元素的 onkeyup 属性值周围的刻度更改为单引号或双引号。

INCORRECT不正确

<input type="number" onkeyup=`addAll()` class="input-field" id="chiller-1" name="chiller-1" placeholder="Input number in KW" value="" required><br>

FIXED固定的

<input type="number" onkeyup="addAll()" class="input-field" id="chiller-1" name="chiller-1" placeholder="Input number in KW" value="" required><br>

By fixing those characters, the code will run.通过修复这些字符,代码将运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM