javascript - 从 HTML 中获取数据并存储到 $(window).load()
问题描述
HTML:
<form>
<input type="text" id = "userText"/>
<button onclick="getData()">Send</button>
</form>
Javascript:
var inputUser;
// I tried this and it didn't work either
function getData(){
inputUser = document.getElementById("userText").value;
alert(inputUser);
}
$(window).load(
function(){ * something * })
我可以在函数 getData() 中获取数据,但无法将该数据访问到 $(window).load(function())。
如果我放入document.getElementById()
或尝试使用inputUser
* something * 代码。什么都不回。我想在 * something * 中使用用户输入的文本。
那么我该怎么做呢?谢谢!
解决方案
load 事件在用户加载文档时触发。
此时,用户还没有在表单中输入任何内容。
您无法在该函数中获取文本输入,因为它尚不存在。
推荐阅读
- sql - 如何根据 SQL 列生成动态产品描述?
- pagination - 如何在 Api 上实现分页并一次只获取一些数据块
- java - 尝试打开项目时获取“项目文件夹已包含 NetBeans 项目”
- python - Jupyter 绘制了意想不到的数字
- powershell - 带有特殊字符的字符串连接
- github - 是否可以仅撤销 GitHub 中 VSCode 的特定权限而不是全部访问权限?
- r - 使用 R connect 部署 Plumber API 时遇到问题
- android - 如何确定用于创建 apk 的 dx/d8 版本?
- python - 文件下载器和格式名称的 Scrapy 问题
- jupyter-notebook - 如何在 jupyter lab 中调整单元格宽度?