如何在页面加载时直接使用Javascript在输入字段中填写数据?

问题描述:

这个问题在Stack Overflow上已经被问了很多,但是没有一个解决方案在起作用.我正在开发一个Web应用程序,在该应用程序中,我必须在页面加载时在数据字段中填写数据.这是我的代码:

This question has been asked a lot it seems on Stack Overflow but none of the solutions seem to be working. I am developing a web application where I have to fill in data in data fields on page load. Here is my code:

<!DOCTYPE html>
<html>
<body>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<input type="text" id="datetime" value="" />
<script type="text/javascript">
$(document).on("pageload",function(){
//for fill field
document.getElementById("datetime").value = "here is value";
});
</script>


</body>
</html>

由于某种原因,当我加载页面时,没有数据被填充,有人看到它的原因吗?

For some reason, when I load the page, no data gets filled in, does anyone see the reason for it?

您不需要jQuery.试试这个:

You don't need jQuery. Try this:

<!DOCTYPE html>
<html>

<body>
  <input type="text" id="datetime" value="" />

  <script>
    window.onload = function() {
      document.getElementById('datetime').value = 'here is value';
    };
  </script>

</body>

</html>