【问题标题】:After Drag and Drop datepicker not showing Calendar拖放日期选择器后不显示日历
【发布时间】:2016-06-28 07:37:09
【问题描述】:

我正在使用 jqueryUI 开发 DnD html5。一切正常,只有一个问题。我在输入文本字段上使用日期选择器。如下:

<input type="text"  class="date" name="date" id="date" placeholder="&nbsp;&nbsp;DD-MM-YEAR">

jquery:

$( ".date" ).datepicker();

这是有效的。但是当我拖动它(克隆)并将其放置在另一个 div 中时,日历没有显示。请任何人帮助我。

【问题讨论】:

  • 你能提供同样的工作样本吗?在 jsfiddle 中?
  • @patilprashant6792:这是非常大的代码,我无法发布它,我尝试发布一小部分代码,但它无法正常工作。请假设我在问题中解释的情况,请给出任何想法如何工作。
  • @patilprashant6792 :按照你我做了以下: $(this).find("input.date").removeClass('hasDatepicker').removeData('datepicker').datepicker(); that is working , calendar is showing but when select date that is filling there where from I clone it.
  • 我已经更新了代码,看看它是否对你有帮助。我认为这是克隆日期选择器的最佳方法
  • 部分问题是当您有一个 Draggable 克隆时,该克隆可能没有绑定 Datepicker。您还忘了提到您正在使用 Draggable 和 Drop 将字段附加到页面。

标签: jquery html jquery-ui datepicker


【解决方案1】:

请尝试以下代码来克隆 datePicker。 HTML

 <div id="someDiv">
   <input class="datefield" />
  </div>

脚本

 $(document).ready(function() {
  var mydiv = $('#someDiv');
  mydiv.find('input.datefield').datepicker();
  var newDiv = mydiv.clone(false).attr("id", "someDiv2").insertAfter(mydiv);
  newDiv.find('input.datefield')
    .attr("id", "")
    .removeClass('hasDatepicker')
    .removeData('datepicker')
    .unbind()
    .datepicker();

})

如果对你有帮助,请更新我

【讨论】:

  • 其实我已经实现了克隆对象的拖放。如果我实现您的代码,那么它就有双重实现 DnD 。为什么我们不能改变已经实现的代码。以下语句有什么问题: $(this).find("input.date").removeClass('hasDatepicker').removeData('datepicker')‌​.datepicker();
  • 我并不是说要按原样实现代码......我是说使用相同的实现只是为了克隆。或者作为参考。而且您所说的代码与帖子具有相同的逻辑。我需要您参考上面的代码并根据您的要求进行成型
【解决方案2】:

这是我的 Jquery 完整快照:

$(document).ready(function() {
        $( ".date" ).datepicker(); //Data Picker
  $('.test').draggable({
    helper: "clone",
    revert: "invalid"

  });
  $('#drop').droppable({
    accept: ".test",
    drop: function(e, u) {
      var a = u.helper.clone();
        count++;

      console.log("INFO: Accepted: ", a.attr("class"));
      a.css("z-index", 1000);
      a.appendTo("#drop");
      a.attr('class', 'dropped').draggable({
        containment: "#drop"

      }).dblclick(function() {
        // Enabled Resize on element when double clicked
          $(this).resizable(); 

          $(this).find("input.date").removeClass('hasDatepicker').removeData('datepicker').unbind().datepicker();
         $(this).find("textarea").replaceWith(function() {
             return '<span>'+$(this).val()+'</span>';
             $(this).find("input[type=text]").attr("name",count);
         });

      });
    }
  });
  $(document).click(function() {
    if ($(".dropped").length) {
      // Disabled Resize on all elements when #drop
      $(".ui-resizable").resizable("destroy");
    }
  });
});
    function formsubmit(){
        alert(document.getElementById('dd').innerHTML);
    }

【讨论】:

    【解决方案3】:

    这里发生了一些引起问题的有趣事情,但我确实找到了解决方案。

    工作示例:https://jsfiddle.net/Twisty/djecvLdf/3/

    HTML 片段

    <div class="elements">
      <div class="new button-wrapper element ui-widget-content ui-corner-all" data-item-type="button">
        <span class="side-handle ui-icon ui-icon-grip-dotted-vertical"></span>
        <button class="" title="">New Button</button>
      </div>
      <div class="new textbox-wrapper element ui-widget-content ui-corner-all" data-item-type="input[type='text']">
        <span class="side-handle ui-icon ui-icon-grip-dotted-vertical"></span>
        <input type="text" class="datepicker" title="" placeholder="New DatePicker" />
      </div>
      <div class="new label-wrapper element ui-widget-content ui-corner-all" data-item-type="label">
        <span class="side-handle ui-icon ui-icon-grip-dotted-vertical"></span>
        <label class="" title="">New Label</label>
      </div>
    </div>
    <div id="drop"></div>
    

    jQuery 片段 1

    在我们的.ready() 函数中,我们查看datepicker 项目是否已被删除:

      $("#drop").on("drop", function() {
        if ($("#drop input.datepicker").length) {
          $("#drop input.datepicker").datepicker();
        }
      });
    

    jQuery 代码片段 2

    在drop 函数内部,我们也会采取这一步:

      if (a.find("input.datepicker").length) {
        var datepick = $("#" + a.find("input").attr("id"));
        datepick.datepicker();
      }
    

    这似乎涵盖了所有的基础。请查看示例并将其实现到您自己的代码中。

    【讨论】:

    • 我像你说的那样放置,但现在我的所有项目都没有下降,只是拖动。
    • @vibog 听起来像是您的 drop 函数中的问题。请检查您的浏览器控制台是否有错误。将您的脚本与工作示例进行比较以了解差异。
    • 我会这样做的。但是请你能给我一些想法,我如何在我的 js 中更新名称、id、drop 元素后的值。谢谢 !! :)
    • @vibog 这已经包含在您的最后一个问题中,并显示在我的工作示例中。如果您仍然无法理解,我会发布另一个问题,因为它与您当前的问题无关。当然你也可以研究这个问题,有很多关于这个主题的可用信息。
    【解决方案4】:

    我做错了什么:

    $(document).ready(function() {
    
      $('.test').draggable({
        helper: "clone",
        revert: "invalid"
    
      });
            $("#drop").on("drop", function() {
        if ($("#drop input.datepicker").length) {
          $("#drop input.datepicker").datepicker();
        }
      })
      $('#drop').droppable({
        accept: ".test",
        drop: function(e, u) {
          var a = u.helper.clone();
            count++;
    
          console.log("INFO: Accepted: ", a.attr("class"));
          a.css("z-index", 1000);
          a.appendTo("#drop");
          a.attr('class', 'dropped').draggable({
            containment: "#drop"
    
          }).dblclick(function() {
            // Enabled Resize on element when double clicked
              $(this).resizable(); 
    
              $(this).find("input.date").removeClass('hasDatepicker').removeData('datepicker').unbind().datepicker();
             $(this).find("textarea").replaceWith(function() {
                 return '<span>'+$(this).val()+'</span>';
                 $(this).find("input[type=text]").attr("name",count);
             });
    
          });
        }
      });
      $(document).click(function() {
        if ($(".dropped").length) {
          // Disabled Resize on all elements when #drop
          $(".ui-resizable").resizable("destroy");
        }
      });
    });
        function formsubmit(){
            alert(document.getElementById('dd').innerHTML);
        }
    

    【讨论】:

      猜你喜欢
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 2014-12-25
      • 1970-01-01
      • 2021-10-16
      • 2021-10-13
      • 1970-01-01
      • 2013-04-27
      相关资源
      最近更新 更多