首页 > 解决方案 > 如何等待输入在 html 页面中填写,然后在 javascript 中将其值打印到控制台?

问题描述

我有一个带有输入字段的 HTML 页面
有人在其中输入了一些文本
他们单击了一个按钮

我想在他们单击带有一些 JS 代码(客户端)的按钮后获取输入字段的值,然后将其打印到控制台/将其保存到文件中。

我该怎么做呢?我试过找,但我根本找不到这样的东西:/

谢谢!:)

标签: javascripthtml

解决方案


尝试这个:

// This function is called by the HTML code onclick on the button
var get_content_of_input = function(){
    var user_input = document.getElementById("text_field").value;
    // Now you can use the variable user_input containing the text
}
<input id="text_field">Please enter Text</input>
<button id="button" onclick="get_content_of_input()">Click here to sumbit</button>

文本字段的内容现在将保存在变量“user_input”中。


推荐阅读