-->

g.size() is not a function - Jquery Error

2020-07-02 09:11发布

问题:

I have been trying to use bootstrap datetimepicker and have all the components on my scripts like Moment JS, bootstrap-date-time js, and bootstrap js but I am getting g.size() is not a function error. I am trying to run the following function

$(function(){
    $('#datetimepicker').datetimepicker();
  });

回答1:

I had the same error as you, and I could not find a solution for it anywhere. In fact the only reference to the g.size() is not a function error was this question, at the time with only a single down vote.

My solution was to find a different datetime picker that actually worked

I found this one: https://github.com/xdan/datetimepicker, it was quite easy to get working:

  1. download the zip from github

  2. Include the files :

    <link href="./js/datetimepicker-master/build/jquery.datetimepicker.min.css" rel="stylesheet"> <script src="./js/datetimepicker-master/build/jquery.datetimepicker.full.min.js"></script>

  3. This HTML

    <input id="datetimepicker" type="text" />

  4. And this JS

    $.datetimepicker.setLocale('en');
    $('#datetimepicker').datetimepicker();

I got this up and working in a tiny fraction of the amount of time I'd wasted trying to get the bootstrap datetimepicker to stop giving me the g.size() error.

Of course there are many other datetime pickers available:

See this post for many more options : whats-a-good-javascript-time-picker



回答2:

.size() was removed in version 3 of jQuery, use .length instead.

In your example, replace g.size() for g.length.



回答3:

The g.size error is known by the developers and exists with newer versions of jquery.

If you wish to use that datetimepicker then go back to jquery 2.1.4 or earlier.

The bug is listed here :

https://github.com/Eonasdan/bootstrap-datetimepicker/issues/1714


回答4:

Please use latest bootstrap-datetimepicker to get rid of this issue. I have used below version and the error is gone.

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/css/bootstrap-datetimepicker.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/js/bootstrap-datetimepicker.min.js"></script>

jQuery v3.3.1 is used.



回答5:

Check your version of jQuery. I was using the latest(3.1.1) some plugins i use were throwing this error.

according to the docs: The .size() method is deprecated as of jQuery 1.8. Use the .length property instead.

( it was removed in 3.0 )

so a simple rollback to a version before 3.0 did the trick.

edit: whoops, as A. Wolff says in the comments.

edit 2: another thing... if you want to keep your jquery version up to date, you could include the jquery-migrate plugin to your project - https://github.com/jquery/jquery-migrate