简体   繁体   English

jQuery datepicker无法在添加的div中工作

[英]jQuery datepicker not working in added div

I have a form (4 inputs) including a jQuery datepicker. 我有一个表单(4个输入),包括一个jQuery日期选择器。 It also has a button that adds another div (also via jQuery, same 4 inputs) below so the user can add multiple records.. The datepicker works in the first form but not any subsequent ones... 它还有一个按钮,在下面添加另一个div(也通过jQuery,相同的4个输入),这样用户就可以添加多个记录.. datepicker工作在第一种形式,但不是任何后续的...

View 视图

<div id="single_module">
<div class="pitch">
<h2>Step 3: Friends Birthdays</h2>
</div>
<form id="myForm">
<div id="input1" style="margin-bottom:4px;" class="clonedInput">
<%= simple_form_for @user, url: wizard_path do |f| %>

  <div class="inputs">
  <ul class="testss1">
  <%= f.simple_fields_for :friends do |friend_f| %>
    <li>
    <%= friend_f.input :name %>
    </li>
    <li>
    <%= friend_f.input :dob, :as => :date_picker, :label => 'Birthday' %>
    <li>
    <%= friend_f.input :gender, :collection => ['male','female'] %></li>
    </li><li>
    <%= friend_f.association :interests, :as => :select, :label => false %></li>


    <%end%>

    </div>
    <div>
    <input type="button" id="btnAdd" value="add another name" />
    <input type="button" id="btnDel" value="remove name" />
    </div>
    </form>
    <div class="actions">
    <%= f.button :submit, 'Next Step', :class => 'btn btn-primary',  %>
    <br></br>
    </div>
    <%end%>

Jquery: jQuery的:

Datepicker 日期选择器

$(document).ready(function(){
    $(function() {
            $( "#user_friends_attributes_0_dob" ).datepicker();

        });
});

Addnewform: Addnewform:

$(document).ready(function() {
           $('#btnAdd').click(function() {
               var num     = $('.clonedInput').length;
               var newNum  = new Number(num + 1);

               var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);

               newElem.children(':first').attr('id', 'name' + newNum).attr('name', 'name' + newNum);
               $('#input' + num).after(newElem);
              $('#btnDel').attr('disabled',false);

               if (newNum == 5)
                   $('#btnAdd').attr('disabled',true);
           });

           $('#btnDel').click(function() {
               var num = $('.clonedInput').length;

               $('#input' + num).remove();
               $('#btnAdd').attr('disabled',false);

               if (num-1 == 1)
                   $('#btnDel').attr('disabled',true);
           });

           $('#btnDel').attr('disabled',true);
       });

You'll want to 'tell' jQuery to dynamically handle a DOM event for your targeted elements and rebind that datepicker functionality. 您需要“告诉”jQuery动态处理目标元素的DOM事件并重新绑定该日期选择器功能。 Start by adding a css class to any of your date picker elements for example "my-datepicker". 首先将css类添加到任何日期选择器元素,例如“my-datepicker”。 Then tell the DOM to bind the datepicker functionality on an event (for example: mousenter): 然后告诉DOM在事件上绑定datepicker功能(例如:mousenter):

$(function() {
     $("#myForm").delegate('.my-datepicker', 'mouseenter', function(){
        $(this).datepicker();
     });
})

The above will tell the DOM to respond to any mouseenter event for elements with the class 'my-datepicker' that are located inside the element with the ID of "myForm" -- and doing so will add the datepicker functionality to that element. 上面将告诉DOM响应任何具有类'my-datepicker'的元素的mouseenter事件,这些元素位于ID为“myForm”的元素内 - 并且这样做会将datepicker功能添加到该元素。

I also noticed that you have some redundancy in your script, the lines: 我还注意到你的脚本中有一些冗余,行:

 $(document).ready(function(){...})

and

 $(function(){...})

do exactly the same thing, but you have them nested in each other. 做同样的事情,但你把它们互相嵌套。 I'm not sure if that's going to cause you problems or not, but I'd only use one or the other. 我不确定这是否会导致你的问题,但我只会使用其中一个。

Note, you could also use jQuery.live() or jQuery.on() depending on which version of jQuery you are running. 注意,您还可以使用jQuery.live()或jQuery.on(),具体取决于您运行的jQuery版本。 .delegate() works for 1.4.3+ and .on() for 1.7+ .delegate()适用于1.4.3+和.on()适用于1.7+

The easiest way - call again datepicker() to newly created date field in the end of $('#btnAdd').click . 最简单的方法 - 再次将datepicker()调用到$('#btnAdd').click末尾新创建的日期字段$('#btnAdd').click Also, there are similar question with a more correct solutions: Why does jQuery UI's datepicker break with a dynamic DOM? 此外,还有类似的问题和更正确的解决方案: 为什么jQuery UI的datepicker会破坏动态DOM? .

And you are reinvents the wheel: in your case you can use https://github.com/ryanb/nested_form or https://github.com/nathanvda/cocoon . 而你正在重新发明轮子:在你的情况下你可以使用https://github.com/ryanb/nested_formhttps://github.com/nathanvda/cocoon (but you still have to solve the problem with dynamic datepicker) (但你还是要用动态的datepicker解决问题)

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

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