javascript - 如何等待输入在 html 页面中填写,然后在 javascript 中将其值打印到控制台?
问题描述
我有一个带有输入字段的 HTML 页面
有人在其中输入了一些文本
他们单击了一个按钮
我想在他们单击带有一些 JS 代码(客户端)的按钮后获取输入字段的值,然后将其打印到控制台/将其保存到文件中。
我该怎么做呢?我试过找,但我根本找不到这样的东西:/
谢谢!:)
解决方案
尝试这个:
// 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”中。
推荐阅读
- r - JaroWinkler 方法 --> 识别字符串中的字符/数字点
- javascript - 如何在控制台中显示闭包函数的内容?#javascript
- python - Python列表和字典理解
- activemq-artemis - ActiveMQ Artemis Web 控制台
- python - 尊重 django 表的顺序
- azure-data-factory - 如何优化 Azure Data FactoryPipeline?
- javascript - 如何使用 React/Styled-components 继承样式
- r - 加权 cumsum 与另一列滚动
- c# - UseInMemoryDatabase ASP.NET Core 3.1 遇到问题
- mysql - 递归表上的 SQL 触发器