1

日期选择器初始化 Sombody 时未调用 getDefault_Date() 函数请解释错误在哪里

    $('.dtpExpiryDate').datepicker({
        numberOfMonths: 1,
        showButtonPanel: false,
        dateFormat: 'dd/mm/yy',
        showOn: 'both',
        buttonImage: "images/calender.jpg",
        buttonImageOnly: true,
        changeMonth: true,
        changeYear: true,            
        minDate: 0,
        maxDate: '+20Y',
        defaultDate: getDefault_Date()
    });


    function getDefault_Date() {
        var issueDate = $.trim($('.dtpIssueDate').val());
        console.log(issueDate);
        if (issueDate != '') {
            var dateArray = issueDate.split("/");
            dateArray[2] += 10;
            var defaultDate = "'" + dateArray[0] + "/" + dateArray[1] + "/" + dateArray[2] + "'";
            return defaultDate;
        } else {
            return 0;
        }
    }
4

4 回答 4

2

在没有defaultDate.

初始化后,进行计算并设置您的deafultDate.

var ddate="";
var issueDate = $.trim($('.dtpIssueDate').val());
if (issueDate != '') {
    var dateArray = issueDate.split("/");
    dateArray[2] += 10;
    var ddate= "'" + dateArray[0] + "/" + dateArray[1] + "/" + dateArray[2] + "'";

} 
$('.dtpExpiryDate').datepicker( "option", "defaultDate", ddate );

注意:确保您ddate的日期格式正确,即 dd/mm/yy... 您在初始化 datpicker 时指定的 dateFormat ..

defaultDate支持由 dateFormat 选项或相对日期定义的格式的字符串。

浏览日期选择器的 API 文档

http://api.jqueryui.com/datepicker/#option-dateFormat

于 2012-12-11T07:44:59.660 回答
0

该函数应该在 jquery 括号之外,例如

$(document).ready(function(){

$('.dtpExpiryDate').datepicker({
    numberOfMonths: 1,
    showButtonPanel: false,
    dateFormat: 'dd/mm/yy',
    showOn: 'both',
    buttonImage: "images/calender.jpg",
    buttonImageOnly: true,
    changeMonth: true,
    changeYear: true,            
    minDate: 0,
    maxDate: '+20Y',
    defaultDate: getDefault_Date()
});

});

function getDefault_Date() {
    var issueDate = $.trim($('.dtpIssueDate').val());
    console.log(issueDate);
    if (issueDate != '') {
        var dateArray = issueDate.split("/");
        dateArray[2] += 10;
        var defaultDate = "'" + dateArray[0] + "/" + dateArray[1] + "/" + dateArray[2] + "'";
        return defaultDate;
    } else {
        return 0;
    }
}

此外,如果它不起作用,您可以将功能问题放在defaultDate前面,例如

defaultDate: function(){
             var issueDate = $.trim($('.dtpIssueDate').val());
    console.log(issueDate);
    if (issueDate != '') {
        var dateArray = issueDate.split("/");
        dateArray[2] += 10;
        var defaultDate = "'" + dateArray[0] + "/" + dateArray[1] + "/" + dateArray[2] + "'";
        return defaultDate;
    } else {
        return 0;
    }
}
于 2012-12-11T07:31:51.377 回答
0

而不是这个声明

 var defaultDate = "'" + dateArray[0] + "/" + dateArray[1] + "/" + dateArray[2] + "'";

做这个

var defaultDate = new Date(dateArray[0], dateArray[1], dateArray[2]);

如果 defaultDate 不是预期日期,您应该尝试根据日期、月份、年份交换这些 dateArray 项目

于 2012-12-11T07:35:19.973 回答
0

我发现我在 datepicker 初始化时调用该函数,当时 issueate 为空,所以我将函数调用放在 issudate datepicker 的 onclose 事件中,现在一切正常,谢谢大家的回答。

来自 jQuery 文档的脚本

$(function() {
        $( "#from" ).datepicker({
            defaultDate: "+1w",
            changeMonth: true,
            numberOfMonths: 3,
            onClose: function( selectedDate ) {
                $( "#to" ).datepicker( "option", "minDate", selectedDate );
            }
        });
        $( "#to" ).datepicker({
            defaultDate: "+1w",
            changeMonth: true,
            numberOfMonths: 3,
            onClose: function( selectedDate ) {
                $( "#from" ).datepicker( "option", "maxDate", selectedDate );
            }
        });
    });
于 2012-12-11T08:29:44.223 回答