Javascript 如何使用html中的占位符在文本框中显示当前日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27670288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:29:53  来源:igfitidea点击:

how to display current date in textbox using placeholder in html

javascriptjqueryhtml

提问by mourya gm

<html>
<body>
Date: <input type="text" id="txt" placeholder=" "/>
</body>
</html>

What should i specify inside placehoder quotes so that i can view current date in my textbox

我应该在占位符引号内指定什么,以便我可以在我的文本框中查看当前日期

回答by arman1991

These are one of the soultions how to display current date in textbox:

这些是如何在文本框中显示当前日期的解决方案之一:

1. JAVASCRIPT SOLUTION

1. JAVASCRIPT 解决方案

<!DOCTYPE html>
<html>
<body onload="myFunction()">


Date: <input type="text" id="demo"/>

<script>
function myFunction() {
document.getElementById('demo').value= Date();
}
</script>
</body>
</html> 

EDIT

编辑

Instead of value, in the same way by id, you could set the placeholderas you wish:

而不是value,以同样的方式通过 id ,您可以placeholder根据需要设置:

document.getElementById('demo').placeholder= Date();


2. JQUERY SOLUTION

2. 查询解决方案

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body onload="myFunction()">


Date: <input type="text" id="demo"/>

<script>
function myFunction() {

$(document).ready(function(){
  $('#demo').attr("placeholder", Date());
});

}
</script>
</body>
</html> 

I think this will help you.

我认为这会对你有所帮助。