[前端_EasyUI]给easyui的datebox设置默认值,获取不到 的解决方法

//给eayui datebox设置初始值
$("#ctime").datebox("setValue", function(){
var date = new Date();
var ctime = date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate();
return ctime;
});
//获取datebox值
var time = $("#ctime").datebox("getValue");
//HTML
日期:<input id="ctime" class="easyui-datebox" editable="false" style=" 150px;">
这样写,给datebox设置的值,在界面上展示了,但是获取的时候是个空白,
如果再手动去给datebox选一个时间,就能获取到手选的值,
怎样获取到给datebox设置的初始值呢????
 
我也遇到过同样的问题,我觉得是因为这个引起的,你的datebox初始化的时候是在html标签上加的样式,而用的时候是用的js。都用js就会好用的。
$("#ctime").datebox({
value:function(){
var date = new Date();
var ctime = date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate();
return ctime;
}
});
 

-------------------------------------------------------------------------------------

实现代码如下:
$(function () {

$('#txtStartTime').datebox('setValue', getNowFormatDate()); 
$('#txtStopTime').datebox('setValue',getNowFormatDate()); 
SetDateBoxFormat("txtStartTime","txtStopTime");

});

function getNowFormatDate() {
       return new Date().format("yyyy-MM-dd");// hh:mm:ss
   }

引用文件dateFormat.js
--------------------------------------

dateFormat.js :

//调用方法:SetDateBoxFormat("txtStartTime","txtStopTime");
function SetDateBoxFormat() {
    for (var i = 0; i < arguments.length; i++) {
        var txtDateBoxId = arguments[i];
  
        $("#" + txtDateBoxId).datebox({
            value: function () {
                var date = new Date();
                var ctime = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
                return ctime;
            }
        });
    }

}

$(function () {

    //调用方法:new Date().format("yyyy-MM-dd hh:mm:ss")
    Date.prototype.format = function(format) {
        var args = {
            "M+": this.getMonth() + 1,
            "d+": this.getDate(),
            "h+": this.getHours(),
            "m+": this.getMinutes(),
            "s+": this.getSeconds(),
            "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
            "S": this.getMilliseconds()
        };
        if (/(y+)/.test(format))
            format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var i in args) {
            var n = args[i];
            if (new RegExp("(" + i + ")").test(format))
                format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? n : ("00" + n).substr(("" + n).length));
        }
        return format;
    };
});


--------------------------------------

-------------------------------------------------------------------------------------

原文地址:https://www.cnblogs.com/jx270/p/4576758.html