简体   繁体   English

当我使用JQuery创建文本框时,实现日期选择器不起作用

[英]Materialize date picker is not working when I create a textbox using JQuery

I have used Materialize CSS ( https://materializecss.com/ ) Date Picker. 我使用了Materialize CSS( https://materializecss.com/ )日期选择器。 But, I couldn't able to access the date picker when I create the textbox dynamically using JQuery. 但是,当我使用JQuery动态创建文本框时,我无法访问日期选择器。 I'm not sure It's not working. 我不确定这没有用。 But, Can able to access date picker when we create a textbox manually. 但是,当我们手动创建文本框时,可以访问日期选择器。 Please check my code. 请检查我的代码。

Manual textbox creation: 手动创建文本框:

<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>

<input type="text" class="datepicker">


<script>
$(document).ready(function(){
    $('.datepicker').datepicker();
});
</script>

Using my above code can able to access the date picker. 使用我上面的代码可以访问日期选择器。

Dynamic textbox creation 动态文本框创建

<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">

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

<button id="add">add</button>

<div id="createtextbox"></div>

<script>
  $(document).ready(function(){
       $('#add').click(function(){
          $("#createtextbox").append('<input type="text" class="datepicker" value="" />');
       });
       $('.datepicker').datepicker();
  });
</script>

Using this code, I couldn't able to access the date picker. 使用此代码,我无法访问日期选择器。 Please help me to solve this? 请帮我解决这个问题? Thanks in advance :) 提前致谢 :)

You are invoking the datepicker() before the input is being created. 您正在创建input之前调用datepicker() In addition, you are creating a multiple input elements on every element (I assume this is not the required behavior). 另外,您正在每个元素上创建多个输入元素(我假设这不是必需的行为)。

Your flow is basically: 您的流程基本上是:

- Document is ready
-- Invoke datepicker() //There is no datepicker input avaiable here
--- (Click) - Create and append a datepicker input
--- (Click) - Create and append a datepicker input
--- (Click) - Create and append a datepicker input
...

Your code execution order is as follows (look at the comments): 您的代码执行顺序如下(查看注释):

  $(document).ready(function(){ //1- document is ready
       $('#add').click(function(){ //Event listener is being registered but is not fired as no one clicked a button
          $("#createtextbox").append('<input type="text" class="datepicker" value="" />');
       });
       $('.datepicker').datepicker(); //2 invoke datepicker... but wait, there is no input - as no one pressed the button
  });

You should change it to: 您应该将其更改为:

  $(document).ready(function(){ //1- document is ready
       $('#add').click(function(){ //Event listener is being registered, this time when it will be fired it will invoke date picker as it was created in step 2
           $('.datepicker').datepicker();
       });
       $("#createtextbox").append('<input type="text" class="datepicker" value="" />'); //2 Create the input which will be used as the datepicker - BEFORE the click event is fired
  });

Which will make a correct flow of: 正确的流程如下:

- Document is ready
-- Create and append a datepicker input
--- (Click) - Invoke datepicker() //There is datepicker input
--- (Click) - Invoke datepicker() //There is datepicker input
--- (Click) - Invoke datepicker() //There is datepicker input
...

So far you have written correct, The only issue here is that your DOM doesn't know that it has to register all the events associated with new text box. 到目前为止,您已经写对了。这里唯一的问题是您的DOM不知道它必须注册与新文本框关联的所有事件。 simple after you append the textbox you have to activiate these events again. 添加文本框后,您只需重新激活这些事件即可。 below should work 下面应该工作

 $(document).ready(function(){ //1- document is ready $('#add').click(function(){ $("#createtextbox").append('<input type="text" class="datepicker" value="" />'); // you have added the textbox $('.datepicker').datepicker(); // call calender again this time it will pick the new added textbox }); }); 

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

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