jQuery 如何解决无法读取未定义错误的属性“timepicker”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18560021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:56:42  来源:igfitidea点击:

How to resolve Cannot read property 'timepicker' of undefined error

jqueryjquery-ui

提问by Milligran

I am trying to implement the Jquery datetime picker on web page but keep getting the following error:

我正在尝试在网页上实现 Jquery 日期时间选择器,但不断收到以下错误:

Uncaught TypeError: Cannot read property 'timepicker' of undefined

未捕获的类型错误:无法读取未定义的属性“timepicker”

I downloaded the timepicker add on from here:

我从这里下载了时间选择器插件:

http://trentrichardson.com/examples/timepicker/#basic_examples

http://trentrichardson.com/examples/timepicker/#basic_examples

http://trentrichardson.com/examples/timepicker/jquery-ui-timepicker-addon.js

http://trentrichardson.com/examples/timepicker/jquery-ui-timepicker-addon.js

Here is the section of the code generating the error:

这是生成错误的代码部分:

$.ui.timepicker = $.ui.timepicker || {};
if ($.ui.timepicker.version) {
    return;
}

On my web page I have the following:

在我的网页上,我有以下内容:

<script type="text/javascript" src="/Scripts/jquery-lib/jquery-2.0.3.js"></script>

<script type="text/javascript"  src="/Content/bootstrap/js/jquery-ui-timepicker-addon.js"></script>
<script type="text/javascript"  src="/Content/bootstrap/js/jquery.ui.datepicker.js"></script>
 <script type="text/javascript"  src="/Content/bootstrap/js/jquery-ui-sliderAccess.js"></script>

And script:

和脚本:

<script>
    $(function () {
        $("#datetimepicker").datepicker();
    });

回答by Vincent Vieira

Uncaught TypeError: Cannot read property 'timepicker' of undefined means that

未捕获的类型错误:无法读取未定义的属性“timepicker”意味着

$.ui

is undefined. So, you probably have not included jQuery UI. Try to include it before any of your scripts (except jQuery itself).

未定义。因此,您可能还没有包含 jQuery UI。尝试在任何脚本之前包含它(jQuery 本身除外)。

回答by tymeJV

You'll also have to import the jQuery lib to use jQuery plugins. Throw this script before the rest:

您还必须导入 jQuery 库才能使用 jQuery 插件。在剩下的之前抛出这个脚本:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>