开发者

jQuery daterangepicker: executes alert twice?

I want it to alert "ok" when you change/make a date. But i get two alerts instead of one?

How can i fix this?

here's the code:

$(document).ready(function(){
       $('#date开发者_C百科Picking').daterangepicker({
           arrows:true,
           onChange: function(){                  
               alert('ok');
           }
       }); 
});

I am using jquery 1.3.2 and UI 1.7.2


It appears that this is known issue with the plugin.

From Scott (Filament) at filamentgroup:

I added a callback function for onChange now that you might be able use. Keep in mind that it may not fit your needs exactly because it will fire on EVERY change that occurs in the input. One-click range shortcuts will actually fire 2 change callbacks because this plugin simply triggers date changes on each datepicker, one at a time. Once this plugin is adopted by jQuery UI, I’m sure we’ll work that part out and have the rangepicker firing events as one widget. For now, you’ll need to set up your page with this issue in mind.

A workaround for this might be to use $('#datePicking').blur() instead of the onChange option of the plugin.


Its better to use onClose instead of onChange

 $(document).ready(function(){
    $('#datePicking').daterangepicker({
       arrows:true,
       onClose : function(){                  
           alert('ok');
       }
   }); 
});

try this it may help


In my case, I am using daterangepicker with angular. My purpose is to watch any change in the model that stores the date range value and save it for an AJAX call later. I face the same issue as it hits the event twice whenever the date is change even if it is only 'Today': Once it is object with startDate and endDate properties, and the other time it is a string.

It can be made use of as an advantage.

 $scope.$watch(
    'rangeOfDate',
    function (newValue) {
        // Due to a known bug of open source library daterangepicker, the event is hit twice 
        //upon change. Once it is an object, and once it is a string. So, use appropriately.
        var selectedDateRange = new Object();
        if (typeof (newValue) == 'object') {
            selectedDateRange.startDate = new Date(newValue.startDate).toLocaleDateString();
            selectedDateRange.endDate = new Date(newValue.endDate).toLocaleDateString();
            //Do as you wish with this custom object
        }
        else if (typeof (newValue) == 'string') {
            alert("string");
        }               
    },
    false);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜