简体   繁体   English

jQuery datetimepicker第一次无法使用

[英]jQuery datetimepicker not work on first time

I have a dynamic time field in which I used jQuery Datetime picker . 我有一个动态时间字段,在其中使用了jQuery Datetime picker

For dynamic initialization I used this code: 对于动态初始化,我使用了以下代码:

 $("body").on("focus",".time-input",function(){
        $(this).datetimepicker();
 });

But it works when I focus for second time. 但是,当我第二次专注时,它会起作用。

A working demo would be great, if possible. 如果可能的话,可以进行演示的工作很好。

The issue is because you only instantiate the date picker on the element the first time it gets focussed, and then every time after, which isn't a good idea. 问题是因为您仅在元素第一次获得焦点时才实例化日期选择器,然后在之后每次都实例化,这不是一个好主意。 Instead you only need to instantiate it on the element on load: 相反,您只需要在加载的元素上实例化它:

$(function() {
    $('.time-input').datetimepicker();
});

If the .time-input element is dynamically appended to the DOM (as I am assuming given the delegated event handler) then you need to call the datepicker() method on it at the point it's added to the DOM. 如果.time-input元素是动态附加到DOM上的(我假设已委派委托事件处理程序),则需要在将它添加到DOM的那一点上调用datepicker()方法。 For example: 例如:

$.ajax({
    url: 'mypage.php',
    success: function(data) {
        if (data.prop) {
            var $input = $('<input class="time-input" />').appendTo('#myForm');
            $input.datepicker();
        }
    }
});

It is because the focusin event is used by the plugin to show the datepicker, the better solution is to initialize the plugin soon after the input elements are created, but if you can't do that, you can look at some workaround like 这是因为插件使用focusin事件来显示日期选择器,更好的解决方案是在创建输入元素后立即初始化插件,但是如果不能这样做,则可以查看一些解决方法,例如

 $("body").on("focus", ".time-input", function() { if (!$(this).data('xdsoft_datetimepicker')) { $(this).datetimepicker({ onGenerate: function() { if (!$(this).hasClass('initial')) { $(this).addClass('initial').triggerHandler('open.xdsoft'); } } }); } }); $('button').click(function() { $('#ct').append('<input class="time-input" />'); }); 
 <link href="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.4.5/jquery.datetimepicker.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.4.5/jquery.datetimepicker.js"></script> <div id="ct"> <input class="time-input" /> </div> <button>Add</button> 

Initialize datatimepicker on DOM element on document ready 准备好文档上的DOM元素上的datatimepicker

$(function () {
    $("body").on("focus",".time-input",function(){
       $(this).datetimepicker();
    });
}

This will solve your problem. 这样可以解决您的问题。

Try "$(document.body)" instead "$(body)". 尝试使用“ $(document.body)”代替“ $(body)”。

$(document.body).on('focus', '.time-input', function(){
    $(this).datetimepicker();
});

I think you need to set datepicker after your dom is created.Here your code is not working because jquery datetimepicker not open the tab when initialized. 我认为您需要在创建dom之后设置datepicker。此处您的代码无法正常工作,因为jquery datetimepicker初始化时未打开选项卡。 and second time you focus its work because its already initialized. 第二次,因为它已经初始化,所以重点关注它的工作。

like this: 像这样:

//here your dom should be updated you can console the length 
//console.log($('.time-input').length)
$('.time-input').each(function(){
  $(this).datetimepicker();
});

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

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