【问题标题】:Implementing jQuery DatePicker in Bootstrap modal在 Bootstrap modal 中实现 jQuery DatePicker
【发布时间】:2014-01-30 07:52:38
【问题描述】:

为我的问题创建了 jsfiddle http://jsfiddle.net/sudiptabanerjee/93eTU/

模式窗口中的问题是更改月份和更改年份组合。

a) IE 11:一切正常 b) Chrome 版本 31,月组合选择,引导模式隐藏。 c) Firefox v26,月份和年份下拉菜单不起作用。

请帮忙。

HTML

<!-- Button trigger modal -->
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">Launch demo modal</button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
             <h4 class="modal-title" id="myModalLabel">Modal title</h4>

        </div>
        <div class="modal-body">
            <div class="col-md-12">
                <div class="row">
                    <label for="idTourDateDetails">Tour Start Date:</label>
                    <div class="form-group">
                        <div class="input-group">
                            <input type="text" name="idTourDateDetails" id="idTourDateDetails" readonly="readonly" class="form-control clsDatePicker"> <span class="input-group-addon"><i id="calIconTourDateDetails" class="glyphicon glyphicon-th"></i></span>

                        </div>
                    </div>Alt Field:
                    <input type="text" name="idTourDateDetailsHidden" id="idTourDateDetailsHidden">
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>
<!-- /.modal -->

CSS

.clsDatePicker {
z-index: 100000;
}

JS

 $('#idTourDateDetails').datepicker({
 dateFormat: 'dd-mm-yy',
 minDate: '+5d',
 changeMonth: true,
 changeYear: true,
 altField: "#idTourDateDetailsHidden",
 altFormat: "yy-mm-dd"
});

【问题讨论】:

    标签: jquery twitter-bootstrap twitter-bootstrap-3 jquery-ui-datepicker


    【解决方案1】:

    这是因为模态强制关注自身。这是here 提到的解决方案。将以下脚本添加到您的 js 文件中。就是这样。

    Working Demo

    jQuery

    // Since confModal is essentially a nested modal it's enforceFocus method
    // must be no-op'd or the following error results 
    // "Uncaught RangeError: Maximum call stack size exceeded"
    // But then when the nested modal is hidden we reset modal.enforceFocus
    var enforceModalFocusFn = $.fn.modal.Constructor.prototype.enforceFocus;
    
    $.fn.modal.Constructor.prototype.enforceFocus = function() {};
    
    $confModal.on('hidden', function() {
        $.fn.modal.Constructor.prototype.enforceFocus = enforceModalFocusFn;
    });
    
    $confModal.modal({ backdrop : false });
    

    对于 Bootstrap 4:

    replace : $.fn.modal.Constructor.prototype.enforceFocus
    By: $.fn.modal.Constructor.prototype._enforceFocus
    

    【讨论】:

    • 只添加了这个 $.fn.modal.Constructor.prototype.enforceFocus = function() {};它工作正常谢谢jsfiddle.net/sudiptabanerjee/93eTU/22。但是您能否提供有关其他代码的更多详细信息以及为什么需要它。
    • @SudiptaBanerjee 见 this post
    • 我进入浏览器(未定义confModal)
    • 感谢您的解决方案
    • 只添加这个:$.fn.modal.Constructor.prototype.enforceFocus = function() {};应该管用。不需要休息:)
    【解决方案2】:

    jQuery@crftr 答案的版本

    var enforceModalFocusFn = $.fn.modal.Constructor.prototype.enforceFocus;
    $.fn.modal.Constructor.prototype.enforceFocus = function() {};
    try{
        $confModal.on('hidden', function() {
            $.fn.modal.Constructor.prototype.enforceFocus = enforceModalFocusFn;
        });
        $confModal.modal({ backdrop : false });
    }
    catch (error) {
        if(error.name != 'ReferenceError')
            throw error;
    }
    

    【讨论】:

    • 你成就了我的一天。它还使用动态生成的模态,这些模态通过 ajax 获取它们的标记。我已将它放在 document.ready 函数中,其中我有一个用于单击模式链接的处理程序,它开箱即用。非常感谢。
    【解决方案3】:

    更简单...只需将此行注释到您的 boostrap.js that.enforceFocus() 中。

    【讨论】:

    • 在库中进行更改对您来说可能看起来更容易,但如果您想在之后使库保持最新状态,这可能会成为一场噩梦。
    【解决方案4】:

    根据 Surjith 的回答,我添加了一个 try/catch 块来解决 confModal 未定义的 ReferenceError 异常。

    咖啡脚本:

    enforceModalFocusFn = $.fn.modal.Constructor::enforceFocus
    
    $.fn.modal.Constructor::enforceFocus = ->
    
    try
      $confModal.on "hidden", ->
        $.fn.modal.Constructor::enforceFocus = enforceModalFocusFn
        return
      $confModal.modal backdrop: false
    catch error
      if error.name != 'ReferenceError'
        throw error
    

    【讨论】:

      【解决方案5】:

      将 z-index 添加到您的日期选择器类

      【讨论】:

        【解决方案6】:

        解决此问题的另一种解决方法是删除 div.modal 元素上的 tabindex="-1" 属性。在 bootstrap 4 上测试。

        【讨论】:

          【解决方案7】:

          找到了一种更简单的方法来解决此日期选择器问题。确保JS在jquery之下,jqueryUI lib调用。

          $("body").delegate("#date1", "focusin", function () {
                $(this).datepicker(
                  {
                    dateFormat: "yy-mm-dd",
                    changeMonth: true,
                    changeYear: true
                  }
                );
          });
          
          
          $( "#date1" ).datepicker();
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2013-12-11
            • 2015-12-23
            • 1970-01-01
            • 2018-08-25
            • 1970-01-01
            • 1970-01-01
            • 2015-07-29
            相关资源
            最近更新 更多