简体   繁体   English

$(...)。datepicker不是函数--JQuery - Bootstrap

[英]$(…).datepicker is not a function - JQuery - Bootstrap

I'm trying to include the datepicker for Bootstrap but am getting the following error: 我正在尝试为Bootstrap包含datepicker但是我收到以下错误:

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

I don't see why it's there. 我不明白为什么会这样。 I've looked at other cases of this error, but none match mine. 我看过这个错误的其他情况,但没有一个匹配我的。

HTML: HTML:

<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script src="<?php echo BASE_URL; ?>/js/moment.min.js"></script>
<script src="<?php echo BASE_URL; ?>/js/bootstrap.min.js"></script>
<script src="<?php echo BASE_URL; ?>/js/bootstrap-datetimepicker.min.js"></script>
<script src="<?php echo BASE_URL; ?>/js/main.js"></script>

<div class="col-md-6">
    <div class="form-group">
         <label for="geboortedatum">Geboortedatum:</label>
         <div class="input-group datepicker" data-provide="datepicker">
               <input type="text" name="geboortedatum" id="geboortedatum" class="form-control">
               <div class="input-group-addon">
                   <span class="glyphicon glyphicon-th"></span>
               </div>
         </div>
    </div>
</div>

JS: JS:

$(document).ready(function() {

    $('.datepicker').datepicker({
        format: 'dd/mm/yyyy'
    });
});

UPDATE after adding JQuery UI 添加JQuery UI后更新

It now looks like the following: 现在看起来如下:

添加JQuery UI后更新

Not the right function name I think 我认为不是正确的功能名称

$(document).ready(function() {

    $('.datepicker').datetimepicker({
        format: 'dd/mm/yyyy'
    });
});

还需要包含jquery-ui:

<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

要摆脱看起来很糟糕的日期选择器,你需要添加jquery-ui css

<link rel="stylesheet" type="text/css" href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css">

You need to include jQueryUI 你需要包含jQueryUI

 $(document).ready(function() { $('.datepicker').datepicker({ format: 'dd/mm/yyyy' }); }); 
 <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script> <script src="https://code.jquery.com/ui/1.11.3/jquery-ui.min.js" integrity="sha256-xI/qyl9vpwWFOXz7+x/9WkG5j/SVnSw21viy8fWwbeE=" crossorigin="anonymous"></script> <script src="<?php echo BASE_URL; ?>/js/moment.min.js"></script> <script src="<?php echo BASE_URL; ?>/js/bootstrap.min.js"></script> <script src="<?php echo BASE_URL; ?>/js/bootstrap-datetimepicker.min.js"></script> <script src="<?php echo BASE_URL; ?>/js/main.js"></script> <div class="col-md-6"> <div class="form-group"> <label for="geboortedatum">Geboortedatum:</label> <div class="input-group datepicker" data-provide="datepicker"> <input type="text" name="geboortedatum" id="geboortedatum" class="form-control"> <div class="input-group-addon"> <span class="glyphicon glyphicon-th"></span> </div> </div> </div> </div> 

I had similiar problem. 我有类似的问题。 The quick fix I found is to change: 我找到的快速修复方法是更改​​:

<div class="input-group datepicker" data-provide="datepicker">

to

<div class="input-group date" data-provide="datepicker">
 <script type="text/javascript">

    var options={
            format: 'mm/dd/yyyy',
            todayHighlight: true,
            autoclose: true,
        };

    $('#datetimepicker').datepicker(options);

</script>

I resolved this by arranging the order in which your JS is being loaded. 我通过安排加载JS的顺序解决了这个问题。

You need to have it as jQuery -> datePicker -> Init js 你需要把它作为jQuery - > datePicker - > Init js

Call your JQuery in your header, datePicker script in head below your jquery and Init JS in footer 在你的jquery下面的头部,datePicker脚本和脚注中的Init JS中调用你的JQuery

You can try the following and it worked for me. 您可以尝试以下方式,它对我有用。

Import following scripts and css files as there are used by the date picker . 导入以下脚本css文件,因为日期选择器使用了这些脚本css文件。

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.1/js/bootstrap-datepicker.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.1/css/bootstrap-datepicker3.css"/>
<link rel="stylesheet" href="css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<script src="http://code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

The JS coding for the Date Picker I use. 我使用的日期选择器的JS编码。

<script>
        $(document).ready(function(){
            // alert ('Cliecked');
            var date_input=$('input[name="orangeDateOfBirthForm"]'); //our date input has the name "date"
            var container=$('.bootstrap-iso form').length>0 ? $('.bootstrap-iso form').parent() : "body";
            var options={
                format: 'dd/mm/yyyy', //format of the date
                container: container,
                changeYear: true, // you can change the year as you need
                changeMonth: true, // you can change the months as you need
                todayHighlight: true,
                autoclose: true,
                yearRange: "1930:2100" // the starting to end of year range 
            };
            date_input.datepicker(options);
        });
    </script>

The HTML Coding: HTML编码:

 <input type="text" id="orangeDateOfBirthForm" name="orangeDateOfBirthForm" class="form-control validate" required>
 <label data-error="wrong" data-success="right" for="orangeForm-email">Date of Birth</label>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM