我有一个带有输入字段的HTML页面
有人在其中输入了一些文本
他们点击一个按钮
我希望在他们使用一些JS代码(客户端)单击按钮之后获取输入字段的值,然后将其打印到控制台/将其保存到文件。
我该怎么做呢?我已经找过了,但我根本找不到这样的东西:/
谢谢!:)
发布于 2018-07-20 08:43:39
这个例子应该可以帮助你实现你的目标。
const inputNode = document.getElementById('input');
const buttonNode = document.getElementById('button');
buttonNode.addEventListener('click', () => {
const inputValue = inputNode.value;
// do what ever you wan't
});
<input id="input" type="text" />
<button id="button">Click</button>
发布于 2021-07-13 16:45:08
试试这个:
// 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“中。
https://stackoverflow.com/questions/51433539
复制相似问题