我有一个JS代码,当你改变一个字段,它调用搜索例程。问题是,当Datepicker更新输入字段时,我找不到任何将触发的jQuery事件。

由于某种原因,当Datepicker更新字段时,没有调用更改事件。当日历弹出时,它会改变焦点,所以我也不能使用它。什么好主意吗?


当前回答

这种方法很简单,每次都适用于文本框控件。$ (" # controlID”)。on('changeDate', function() {$(this).change();});

其他回答

我写这篇文章是因为我需要一个解决方案来仅在日期改变时触发事件。

这是一个简单的解决办法。每次对话框关闭时,我们都会测试数据是否发生了变化。如果存在,则触发自定义事件并重置存储的值。

$('.datetime').datepicker({
    onClose: function(dateText,datePickerInstance) {
        var oldValue = $(this).data('oldValue') || "";
        if (dateText !== oldValue) {
            $(this).data('oldValue',dateText);
            $(this).trigger('dateupdated');
        }
    }
});

现在我们可以为那个自定义事件连接处理程序了……

$('body').on('dateupdated','.datetime', function(e) {
    // do something
});
$('#inputfield').change(function() { 
    dosomething();
});

在jQueryUi 1.9上,我已经设法让它通过一个额外的数据值和beforeShow和onSelect函数的组合来工作:

$( ".datepicker" ).datepicker({
    beforeShow: function( el ){
        // set the current value before showing the widget
        $(this).data('previous', $(el).val() );
    },

    onSelect: function( newText ){
        // compare the new value to the previous one
        if( $(this).data('previous') != newText ){
            // do whatever has to be done, e.g. log it to console
            console.log( 'changed to: ' + newText );
        }
    }
});

对我有用:)

DatePicker选择的值更改事件代码如下

/* HTML Part */
<p>Date: <input type="text" id="datepicker"></p>

/* jQuery Part */
$("#datepicker").change(function() {
                selectedDate= $('#datepicker').datepicker({ dateFormat: 'yy-mm-dd' }).val();
                alert(selectedDate);        
            });

你在datepicker对象中寻找onSelect事件:

$('.selector').datepicker({
   onSelect: function(dateText, inst) { ... }
});