Use jQuery DatePicker in Bootstrap 3 modal

后端 未结 3 641
花落未央
花落未央 2021-02-06 17:42

I would like to use jQuery UI datepicker in a modal. The real problem is that if I want to also show years and months, it shows me empty selects:

相关标签:
3条回答
  • 2021-02-06 17:47

    Only had to add this style:

    <style>
    .ui-datepicker{
        z-index: 9999999 !important;
    }
    </style>
    
    0 讨论(0)
  • 2021-02-06 18:02

    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>

    0 讨论(0)
  • 2021-02-06 18:08

    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() {};
    
    0 讨论(0)
提交回复
热议问题