简体   繁体   English

在document.ready中使用时,Bootstrap datetimepicker不是函数

[英]Bootstrap datetimepicker is not a function when used within document.ready

I have certain elements which I am adding dynamically using Jquery. 我有一些我使用Jquery动态添加的元素。 Bootstrap Datepicker works fine on input elements that are already present on page load but doesnt work with dynamically added elements. Bootstrap Datepicker适用于页面加载中已存在的输入元素,但不适用于动态添加的元素。 So I tried using .on function of Jquery. 所以我尝试使用Jquery的.on函数。 But I am getting the following error when I place datepicker bind code anywhere within document ready. 但是当我将datepicker绑定代码放在文档准备好的任何地方时,我收到以下错误。

Error: 错误:

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

Script 脚本

   <script type="text/javascript">
        $(document).ready(function () {
            $('body').on('click', 'div[id^="datetimepicker"]', function () {
                alert();

                $('div[id^="datetimepicker"]').datetimepicker({
                    useCurrent: false 
                });

            });
        });

</script>

UPDATE: 更新:

This doesnt work on any element (datetimepicker is not a function): 这不适用于任何元素(datetimepicker不是函数):

   $('body').on('click', "div[id^='datetimepicker']", function () {
             console.log(this);
             $(this).datetimepicker({
                     useCurrent: false //Important! See issue #1075
                });
        });

This works only on elements that are already present: 这仅适用于已存在的元素:

$('#griddt').find('div[id^="datetimepicker"]').datetimepicker({
        useCurrent: false //Important! See issue #1075
    });

UPDATE 2: (click inside the first textbox to add new row of grid, click inside second to see the calendar - which works only within first row) https://jsfiddle.net/a8j30rcw/1/ 更新2 :(在第一个文本框内单击以添加新的网格行,在第二个内部单击以查看日历 - 仅在第一行内有效) https://jsfiddle.net/a8j30rcw/1/

Whereever you are calling datepicker function use like this:- 无论你使用datepicker函数如何使用如下: -

$('body').on('click',"#datetimepicker", function(){
    $(this).datetimepicker();
});

I can see 2 possibilities 我可以看到两种可能性

1: either datetimepicker script is not loaded yet, 1:还没有加载datetimepicker脚本,

2: datetimepicker id isn't available to jquery 2:datetimepicker id不可用于jquery

for both above case you can try timeout just to see if this is the issue 对于上述两种情况,您可以尝试超时只是为了查看这是否是问题

setTimeout(function(){ 

// call to datepicker 
    $('div[id^="datetimepicker"]').datetimepicker({
           useCurrent: false 
    });

 }, 200);

if this doesnt work it means you have duplicate ids. 如果这不起作用,则意味着您有重复的ID。 instead of id use class to refer to your element 而不是id使用类来引用你的元素

$('.datepickerclass').datetimepicker({
           useCurrent: false 
    });

You should not try to listen on datetime picker based on an id attribute at all. 您根本不应该尝试根据id属性来监听日期时间选择器。 The reason is that adding elements dynamically inside the page may create more than one occurrences of the datetime picker w 原因是在页面内动态添加元素可能会创建多个日期时间选择器w

 $(document).ready(function() { $('.form-group').html(`<div class='datetimepicker input-group date'> <input type='text' class="form-control" /> <span class="input-group-addon"> <span class="glyphicon glyphicon-calendar"></span> </span> </div>`); }); $('body').on('click', '.datetimepicker', function() { var tp = $(this); if (tp.data().DateTimePicker) { return true; } tp.datetimepicker({ useCurrent: false }); tp.data().DateTimePicker.show(); }); 
 /* Optional theme */ @import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css'); @import url('//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css'); @import url('https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css'); @import url(https://cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/e8bddc60e73c1ec2475f827be36e1957af72e2ea/build/css/bootstrap-datetimepicker.css') 
 <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment-with-locales.js"></script> <script src="https://cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/master/build/js/bootstrap-datetimepicker.min.js"></script> <div class="container"> <div class="row"> <div class='col-sm-6'> <div class="form-group"> </div> </div> </div> </div> 

ith the same id . ith相同的id Yet again id has to be unique. 然而, id 必须是独一无二的。

Use a class ( datetimepicker ) or some HTML data-attribute then listen for it. 使用类( datetimepicker )或一些HTML data-attribute然后监听它。 Since the event handles dynamic elements added after page load you don't actually need $(document).ready at all although you may add it without any side effects. 由于事件处理页面加载后添加的动态元素,你实际上并不需要$(document).ready ,尽管你可以添加它而没有任何副作用。

    $('body').on('click', '.datetimepicker', function(){
        var tp = $(this);
        if (tp.data().DateTimePicker) {
            return true;
        }
        tp.datetimepicker({useCurrent: false});
        tp.data().DateTimePicker.show();
    });

Update: On click you just initialized the picker everytime a click occurs. 更新: click您只需在每次点击时初始化选择器。 Never triggered the show method. 从未触发过show方法。 I check if time-picker is initialized during the first click if not, I initialize it and showing the picker. 我检查在第一次点击期间是否初始化时间选择器,如果没有,我初始化它并显示选择器。 In any other case I leave timepicker code to do its job by returning early. 在任何其他情况下,我留下时间戳代码,通过提前返回来完成它的工作。

Update: JSFiddle Example 更新: JSFiddle示例

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

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