【问题标题】:$().datepicker is not a function$().datepicker 不是函数
【发布时间】:2019-06-16 20:56:38
【问题描述】:

我已添加日期选择器功能,但我无法使用它。我相信这是我犯了一些根本性的错误,因为我是 javascript 和 jQuery 的初学者。

输入标签在invite.html文件中,我使用django的模板标签将所有jquery和语义文件包含在静态文件夹中。我还添加了 datepicker.js 文件。提前谢谢。我在 datepicker 脚本中添加并提醒它工作正常,所以我认为这是正确添加的功能,但是

我收到以下错误:

jQuery.Deferred 异常:$(...).datepicker 不是函数
TypeError: $(...).datepicker 不是函数

Scholarship.html 是主要的 html 文件,我在其中包含了邀请.html 文件,输入标签 id="datepicker" 在邀请.html 中以粗体显示,生成的功能以粗体显示。

base.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Fusion!
        {% block title %}{% endblock %}
    </title>

    {% load staticfiles %}
    <link rel="stylesheet" type="text/css" href="{% static 'globals/semantic-ui/components/reset.css' %}">
    <link rel="stylesheet" type="text/css" href="{% static 'globals/semantic-ui/components/icon.css' %}">
    <link rel="stylesheet" type="text/css" href="{% static 'globals/css/semantic.min.css' %}">
    <link rel="stylesheet" type="text/css" href="{% static 'globals/css/mediaquery.css' %}">
    <link rel="stylesheet" type="text/css" href="{% static 'globals/css/semantic-notify.css' %}">

    <script type="text/javascript" src="{% static 'globals/js/jquery.min.js' %}"> </script>
    <script type="text/javascript" src="{% static 'globals/js/jquery.formset.js' %}"> </script>
    <script type="text/javascript" src="{% static 'globals/js/semantic-notify.js' %}"> </script>
    <script type="text/javascript" src="{% static 'globals/js/ajax-post.js' %}"></script>
    {% block css %}
    {% endblock %}

</head>

{% comment %}style="background-color: rgba(189, 189, 189, 0.1);"{% endcomment %}
<body id="body">

{% block body %}
    {% block navBar %}
    {% endblock %}
{% endblock %}

    <div id="messages">
        {% for message in messages %}
            <div class="message" tag="{% if 'success' in message.tags %}green{% elif 'error' in message.tags %}red{% else %} blue {% endif %}" message="{{ message|safe }}"></div>
        {% endfor %}
    </div>
</body>

<script type="text/javascript" src="{% static 'globals/js/semantic.min.js' %}"></script>
<script type="text/javascript" src="{% static 'globals/js/dropdown.js' %}"></script>
<script type="text/javascript" src="{% static 'globals/js/tab.js' %}"></script>

<script>

    $(document).ready(function() {

        $('#messages').find('.message').each(function(){
            $.uiAlert({
                textHead: $(this).attr('message'), // header
                text: '',
                bgcolor: $(this).attr('tag'), // background-color
                textcolor: '#fff', // color
                position: 'bottom-left',// position . top And bottom ||  left / center / right
                time: 3, // time
            });
        });

        $('#new-notification')
            .popup({
                inline: true,
                hoverable: true,
                position: 'bottom left',
                popup: $('#notificationPopup'),
                on: 'click',
                delay: {
                    show: 250,
                    hide: 500
                }
            })
        ;
    });
</script>

Scholarship.html

    {% extends 'globals/base.html' %}
        {% load static %}


        {% block title %}
            Awards & Scholarship
        {% endblock %}


        {% block body %}
            {% block navBar %}
                {% include 'dashboard/navbar.html' %}
            {% endblock %}

                        {% block winners %}
                            {% include 'scholarshipsModule/winners.html' with winners=winners %}
                        {% endblock %}
                    </div>
                    {% comment %}The Personal Details end here!{% endcomment %}


                    {% comment %}The Publications starts here!{% endcomment %}
                    <div class="ui tab segment" data-tab="second">
                        {% block invite %}
                            {% include 'scholarshipsModule/invite.html' with release=release ch=ch time=time awards=awards form=form %}
                        {% endblock %} *invite.html is included here*
                    </div>

                    </div>
                </div>
                {% comment %}The right-rail segment ends here!{% endcomment %}

                {% comment %}The right-margin segment!{% endcomment %}
                <div class="column"></div>

            </div>


        {% endblock %}



        {% block javascript %}
            <script src="https://cdn.rawgit.com/mdehoog/Semantic-UI/6e6d051d47b598ebab05857545f242caf2b4b48c/dist/semantic.min.js"></script>
            <script type="text/javascript" src="{% static 'globals/js/datepicker.js' %}"></script>
            <script type="text/javascript" src="{% static 'globals/js/tablesort.js' %}"></script>
            <script type="text/javascript" src="{% static 'globals/js/editProfile.js' %}"></script>
            <script type="text/javascript" src="{% static 'globals/js/modal.js' %}"></script>
            <script>
                    $('.message .close')
              .on('click', function() {
                $(this)
                  .closest('.message')
                  .transition('fade')
                ;
              })
            ;
                **$( function() {
                    $( "#datepicker" ).datepicker(); This is the code that generates the error
                } );**
                </script>
        {% endblock javascript %}


invite.html
        {% load static %}
        {% block winners %}


                        <div class="two fields">
                            <div class="field">
                                <label>Start date</label>

                                    <div class="ui input large left icon">
                                        <i class="calendar icon"></i>
                                        **<input id="datepicker" type="text" name="From" placeholder="YYYY-MM-DD" required>
                                    </div>**

                            </div>

                            <div class="field">
                                <label>End Date</label>

                                    <div class="ui input large left icon">
                                        <i class="calendar icon"></i>
                                        <input type="text" name="To" placeholder="YYYY-MMM-DD" required>
                                    </div>
                                </div>


                    <div class="ui divider"></div>
                </div>
            </div>
        {% endblock %}

This is the datepicker.js fuction which is their in the globals/js folder 

datepicker.js script
        $('.rangestart').calendar({
            type: 'date',
        });
        $('.rangeend').calendar({
            type: 'date',
        });

        $(".date.calendar").calendar({ type: "date" });

【问题讨论】:

  • 何时/何时包含 jQuery?它必须包含在之前 datepicker.js(和语义UI)
  • @Andreas 我也这么认为,但是如果缺少 jQuery,那么错误将是 $ is undefined。除非将$ 分配给另一个框架......无论哪种方式,这里都没有足够的信息来提供帮助。
  • 这个问题最可能的原因是{% static 'globals/js/datepicker.js' %} 结束了 404ing。检查浏览器开发者工具的网络选项卡。下一个最可能的原因是您加载了两次 jQuery 并使用附加的 datepicker 覆盖了版本。但是,问题中没有足够的信息来确定真正的问题是什么。
  • 你的 datepicker.js 没有加载,可能是这个 {% static 'globals/js/datepicker.js' %} 引起了问题。
  • @RoryMcCrossan 因为 "jQuery.Deferred exception: ..." 必须有一个 jQuery 包含,或者它必须是其中一个包含的一部分。因此我猜错了顺序。

标签: javascript jquery html


【解决方案1】:

您应该将以下脚本添加到您的站点中。之后您的错误将被删除。

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>

【讨论】:

    【解决方案2】:

    $('#date1').datepicker({
        changeMonth: true,
        changeYear: true,
        showButtonPanel: true,
        dateFormat: "m/d/yy"
    });
    #ui-datepicker-div { font-size: 12px; } 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css"></script>
    Date Picker: <input type="text" id="date1" name="date1"/> <br/>

    【讨论】:

      猜你喜欢
      • 2017-10-13
      • 1970-01-01
      • 2016-09-12
      • 2012-03-17
      • 1970-01-01
      • 2020-10-07
      • 1970-01-01
      • 2010-11-15
      相关资源
      最近更新 更多