如何在页面加载时直接使用Javascript在输入字段中填写数据? [英] How to fill in data in an input field using Javascript directly on page load?

查看:73
本文介绍了如何在页面加载时直接使用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>

这篇关于如何在页面加载时直接使用Javascript在输入字段中填写数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆