动态设置Input框的默认值

$("#projectcode").setDefauleValue();

//设置合同编号的默认值
$.fn.setDefauleValue = function() {
var defauleValue = $(this).val();
function p(s) {
return s < 10 ? '0' + s : s;
}
var myDate = new Date();
//获取当前年
var year=myDate.getFullYear().toString().substr(-2,2);
alert(year);
//获取当前月
var month=myDate.getMonth()+1;
//获取当前日
var date=myDate.getDate();
var h=myDate.getHours(); //获取当前小时数(0-23)
var m=myDate.getMinutes(); //获取当前分钟数(0-59)
var s=myDate.getSeconds();
var now=year+p(month)+p(date)+p(h)+p(m)+p(s);
function randomNum(n){
var t='';
for(var i=0;i<n;i++){
t+=Math.floor(Math.random()*10);
}
return t;
}
var randomCode = now+randomNum(4);
defauleValue = randomCode;
$(this).val(defauleValue).css("color","#999");

return this.each(function() {
$(this).focus(function() {
if ($(this).val() == defauleValue) {
$(this).val("").css("color","#000");//输入值的颜色
}
}).blur(function() {
if ($(this).val() == "") {
$(this).val(defauleValue).css("color","#999");//默认值的颜色
}
});
});
}

原文地址:https://www.cnblogs.com/yuner-angel/p/8072690.html