2012-07-23 110 views

回答

78

工作演示http://jsfiddle.net/YbLnj/

文档:http://jqueryui.com/demos/datepicker/

代码

$("#dt").datepicker({ 
    onSelect: function(dateText, inst) { 
     var date = $(this).val(); 
     var time = $('#time').val(); 
     alert('on select triggered'); 
     $("#start").val(date + time.toString(' HH:mm').toString()); 

    } 
}); 
+3

为什么'onSelect'没有被列为datepicker API上的事件? – 2014-04-25 23:09:42

+1

@PabloKarzin它在'api documentation'这里提到':''='http://api.jqueryui.com/datepicker/并且这里=> http://api.jqueryui.com/datepicker/#option-onSelect 。 – 2014-04-26 05:01:49

+8

是@Tats_innit,但不是一个事件。它被分类为一个选项。也许这没有错,但作为一个开发人员,我喜欢将它视为一个事件 – 2014-04-26 19:56:47

0

如果日期选择器是在网格中的行,你可以试试

editoptions : { 
    dataInit : function (e) { 
     $(e).datepicker({ 
      onSelect : function (ev) { 
       // here your code 
      } 
     }); 
    } 
} 
9

使用下面的代码:

$(document).ready(function() { 
    $('.date-pick').datepicker({ 
    onSelect: function(date) { 
     alert(date) 
    }, 
    selectWeek: true, 
    inline: true, 
    startDate: '01/01/2000', 
    firstDay: 1, 
    }); 
}); 

您可以自己调整参数:-)

+0

@inkwai如果我的anwser帮助请接受它,以便其他人可以学习它。如果不是,我可以怎样帮助你呢? – RTB 2012-07-23 13:43:46

5

如果你也有兴趣在用户关闭日期选择对话框,而不选择日期的情况下(在我情况下选择没有日期也意),你可以绑定到onClose事件:

$('#datePickerElement').datepicker({ 
     onClose: function (dateText, inst) { 
      //you will get here once the user is done "choosing" - in the dateText you will have 
      //the new date or "" if no date has been selected    
     }); 
1
$(".datepicker").datepicker().on("changeDate", function(e) { 
    console.log("Date changed: ", e.date); 
}); 
相关问题