首页 > 解决方案 > 从 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 * 中使用用户输入的文本。

那么我该怎么做呢?谢谢!

标签: javascripthtml

解决方案


load 事件在用户加载文档时触发。

此时,用户还没有在表单中输入任何内容。

您无法在该函数中获取文本输入,因为它尚不存在


推荐阅读