我有一个JS代码,当你改变一个字段,它调用搜索例程。问题是,当Datepicker更新输入字段时,我找不到任何将触发的jQuery事件。
由于某种原因,当Datepicker更新字段时,没有调用更改事件。当日历弹出时,它会改变焦点,所以我也不能使用它。什么好主意吗?
我有一个JS代码,当你改变一个字段,它调用搜索例程。问题是,当Datepicker更新输入字段时,我找不到任何将触发的jQuery事件。
由于某种原因,当Datepicker更新字段时,没有调用更改事件。当日历弹出时,它会改变焦点,所以我也不能使用它。什么好主意吗?
当前回答
在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 );
}
}
});
对我有用:)
其他回答
我写这篇文章是因为我需要一个解决方案来仅在日期改变时触发事件。
这是一个简单的解决办法。每次对话框关闭时,我们都会测试数据是否发生了变化。如果存在,则触发自定义事件并重置存储的值。
$('.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
});
我的soluthion:
var $dateInput = $('#dateInput');
$dateInput.datepicker({
onSelect: function(f,d,i){
if(d !== i.lastVal){
$dateInput.trigger("change");
}
}
}).data('datepicker');
$dateInput.on("change", function () {
//your code
});
在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 );
}
}
});
对我有用:)
手册说:
apply.daterangepicker: Triggered when the apply button is clicked, or when a predefined range is clicked
So:
$('#daterange').daterangepicker({
locale: { cancelLabel: 'Clear' }
});
$('#daterange').on('apply.daterangepicker', function() {
alert('worked!');
});
对我有用。
试试这个
$('#dateInput').datepicker({
onSelect: function(){
$(this).trigger('change');
}
}});
希望这对你有所帮助。