Uncaught TypeError: $(…).datepicker is not a function(anonymous function)

后端 未结 6 1385
感情败类
感情败类 2020-12-14 07:22

I found few answers on stack overflow but still cant resolve my problem. I am running on Django but I dont think it is relevant for this error.

I try to make work my

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-14 08:16

    What went wrong?

    When you include jQuery the first time:

    
    
    

    The second script plugs itself into jQuery, and "adds" $(...).datepicker.

    But then you are including jQuery once again:

    
    

    It undoes the plugging in and therefore $(...).datepicker becomes undefined.

    Although the first $(document).ready block appears before that, the anonymous callback function body is not executed until all scripts are loaded, and by then $(...) (window.$ to be precise) is referring to the most recently loaded jQuery.

    You would not run into this if you called $('.dateinput').datepicker immediately rather than in $(document).ready callback, but then you'd need to make sure that the target element (with class dateinput) is already in the document before the script, and it's generally advised to use the ready callback.

    Solution

    If you want to use datepicker from jquery-ui, it would probably make most sense to include the jquery-ui script after bootstrap. jquery-ui 1.11.4 is compatible with jquery 1.6+ so it will work fine.

    Alternatively (in particular if you are not using jquery-ui for anything else), you could try bootstrap-datepicker.

提交回复
热议问题