Implementing jQuery DatePicker in Bootstrap modal

余生长醉 提交于 2019-11-27 04:35:49
Surjith S M

This is because the modal enforces focus on itself. Here is a solution for this as mentioned here . Add the below script to your js file. That's it.

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 });

For Bootstrap 4:

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

jQuery version of @crftr answer

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;
}

MORE EASY... just need to comment this line into your boostrap.js that.enforceFocus().

Building off of Surjith's answer, I added a try/catch block to resolve a ReferenceError exception for confModal being undefined.

Coffeescript:

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

add z-index to your datepicker class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!