Use jQuery DatePicker in Bootstrap 3 modal

 ̄綄美尐妖づ 提交于 2019-12-03 08:44:41
Davide Pastore

I found the solution with this answer. The only option you needed is enforceFocus.

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
$.fn.modal.Constructor.prototype.enforceFocus = function() {};

Only had to add this style:

<style>
.ui-datepicker{
    z-index: 9999999 !important;
}
</style>

i could not get $.fn.modal.Constructor.prototype.enforceFocus = function() {}; to work becuase the 'focusin.bs.modal' event was already attached to the modal. this is the code i got to work; because the date picker is called after the modal opens. using the beforeShow() and onClose function i was able to turn the focus event off and on

var datepicker_opts = {
    changeMonth: true,
    changeYear: true,
    dateFormat: 'mm/dd/yy',
    altFormat: "yymmdd",
    setDate: new Date(),
    maxDate: new Date()					
  };

BootboxContent = function(){    
var frm_str = '<form id="some-form">'
+'<div class="form-group">'
+ '<label for="date">Date</label>'    
+ '<input id="date" class="date span2 form-control input-sm" size="16" placeholder="mm/dd/yy" type="text">'
+ '</div>'
+ '</form>';

  var object = $('<div/>').html(frm_str).contents();

  object.find('.date').datepicker(
    $.extend({
      beforeShow: function(elem, inst) {
        $(document).off('focusin.bs.modal');
      },
      onClose: function(selectedDate, inst) {
        $modal = $(inst.input[0]).closest(".modal").data('bs.modal');
        $modal.enforceFocus();	
      }
    }, datepicker_opts)
   );

   return object
  }
 $('.add_date_btn').on('click', function () {
       bootbox.dialog({
           message: BootboxContent,
           title: "View modal with date",
           buttons: {
               cancelar: {
                   label: "Cancel",
                   className: "btn-default"
               }
           }
       });
   }); //End click
.hidden-form #add_class_form {
  display:none; 
}
.ui-datepicker{
	z-index: 9999999 !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://github.com/makeusabrew/bootbox/releases/download/v4.4.0/bootbox.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet"/>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>




<button class="add_date_btn btn btn-primary btn-sm">View modal with date</button>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!