javascript - JS : How to print a value from a text box into the screen?

标签 javascript innerhtml

我想根据表单中输入(文本框)中的文本将值打印到屏幕上。

<input type="text" name="pesos" value="">
<button onclick="conversorMonedas()">Convertir!</button>
<p id="resultado"></p>

function conversorMonedas() {
    var pesos = document.getElementById("pesos");
    document.getElementById("resultado").innerHTML = pesos.value;
}

当我单击该按钮时,控制台上的错误会短暂出现,然后消失。我设法阅读它,它说 pesos.value 为空。

如何打印我在文本框中写的内容?谢谢!

最佳答案

您没有 id 为 pesos 的元素。将 id 添加到您的输入:

<input type="text" name="pesos" id="pesos" value="">

关于javascript - JS : How to print a value from a text box into the screen?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35348296/

相关文章:

javascript - div onmouseout 不符合预期

javascript - react setState 回调没有更新状态

c# - .ascx 中的 javascript 函数

javascript - 如何在纯javascript中点击时获取li元素内的文本

javascript - 更改类中 anchor 的innerhtml

javascript - 当从函数返回内容时,将新创建的 TD 元素上的 .innerHTML 设置为空白

javascript - 在 React native 移动应用程序中集成 firestore 时获取 'firestore/permission-denied'

javascript - 仅当帧速率允许时才在 html5 canvas 中渲染特效

javascript - 使用innerHTML修改img src

css - 在移动或单击 map 之前,Leaflet 不会在 IE/Safari 中显示 Control.extend 数据