jQuery - how to alter Datepicker settings after it has been initialized

若如初见. 提交于 2020-01-19 05:43:11

问题


I would like to bind to initialized datepicker "onSelect" function.

I've been trying hard to find a solution on the web, but was unsuccessful.

Anyone can tell me how to do it?


回答1:


You can simply use the setter

$('#datepicker').datepicker();
$('#datepicker').datepicker("option", "onSelect", function(){alert('hi')});

fiddle here http://jsfiddle.net/nhmsZ/




回答2:


I found the above solutions caused problems in my case. Setting onSelect after the datepicker has already been initialised seems to cause the datepicker to be reinitialised. This may not be desirable.

If you are having problems it may be necessary to go through the back-door. This could have compatibility issues when JQUI is updated, but if you are willing to deal with that, then this code could help you out...

var inst = $.datepicker._getInst($('#startdate').get(0));
inst.settings.onSelect = function (dt) {
    // your code here
};



回答3:


First off Nicola's answer is correct and helped me. I was finding a few related posts that were not really solving my problem.

I'm writing this to expand on the existing answer. If you use a framework that limits your ability to control the order of execution of your JavaScript, say for example Zend Framework, you might need to fire off that bind option with a delay.

    $(document).ready(function() {
        function imahackfunction() {
            $('#startdate').datepicker('option', 'onSelect', function () {
                alert($('#startdate').val());
            });
        }
        setTimeout(imahackfunction, 2000);
    }); 

The above worked for me as I've got two $(document).ready(function() { blocks, one I open in my view, the other "injected" by the framework which instantiates the datepicker.

What I wonder is why the datepicker does not simply fire the change event on the input associated by the altField property...



来源:https://stackoverflow.com/questions/9806742/jquery-how-to-alter-datepicker-settings-after-it-has-been-initialized

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!