jsp中一个输入框,如何把输入框的默认值value设为当前日期yyyy-mm-dd ?

如题所述

设置当前时间的思路就是先通过java方法获取到当前的时间,之后直接赋值给jsp中“input”中
举例:

<%
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String date= format.format(new Date());
%>
<body>
<input type="text" value="<%=date%>"/>
</body>

解释:由于jsp中会先加载上面的java代码,之后在顺序加载jsp页面代码,所以date值就会显示出获取的当前时间。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-12-29
在后面加这段script就行
<script>
Date.prototype.format = function(format){
var o = {
"M+": this.getMonth() + 1, //month
"d+": this.getDate(), //day
"h+": this.getHours(), //hour
"m+": this.getMinutes(), //minute
"s+": this.getSeconds(), //second
"q+": Math.floor((this.getMonth() + 3) / 3), //quarter
"S": this.getMilliseconds() //millisecond
}
if (/(y+)/.test(format))
format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(format))
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
return format;
}

var a = document.getElementById("d11");
var b = new Date();
a.value=b.format('yyyy-MM-dd');
</script>

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网