JQuery UI datepicker外部函数调用选项不起作用

时间:2012-12-11 07:17:13

标签: jquery function user-interface datepicker

当datepicker初始化时,getDefault_Date()函数不会调用 Sombody请说明错误在哪里

    $('.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 个答案:

答案 0 :(得分:2)

在没有defaultDate的情况下初始化您的日期选择器。

在initailize之后,进行计算并设置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处于正确的dateFormat格式,即dd / mm / yy ...您在初始化数据选择器时指定的dateFormat ..

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

查看datepicker的API文档

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

答案 1 :(得分: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;
    }
}

答案 2 :(得分:0)

而不是这句话

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

这样做

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

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

答案 3 :(得分:0)

我想通了我在datepicker初始化的时候调用了函数,当时的issate是空的,所以我把函数调用放在了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 );
            }
        });
    });