Month select in datepicker inside a bootstrap-modal won't work in Firefox

断了今生、忘了曾经 提交于 2019-12-07 09:44:03

问题


The month select in the datepicker doesn't work in FireFox if it is within a bootstrap-modal.

<button class="btn" id="btn">Click Me</button>

<div class="modal hide" id="modal" tabindex="-1">
    <div class="modal-body">
        <input type="text" id="datepicker" />        
    </div>
</div>

JavaScript:

$("#datepicker").datepicker({"changeMonth": true});

$('#btn').click(function() {
    $("#modal").modal('show');
});

Here is a minified example: http://jsfiddle.net/nKXF2/

I found a simmilar twitter-bootstrap github issue: https://github.com/twbs/bootstrap/issues/5979


回答1:


I found two fixes for this bug:

Fix 1: If you remove the tabindex attr in div.modal, the month select works just fine. The only issue I had with this solution is that, on IE (any version) you still need to double-click the month dropwdown for it to open up.

Fix2: The second solution you can find at: http://jsfiddle.net/nKXF2/1/ By overriding the enforceFocus function which was proposed in this question also, you get the month dropdown to work again.

$('#modal').on('show', function () {
    $.fn.modal.Constructor.prototype.enforceFocus = function () { };
});

I think this second one is the best.




回答2:


I had this same problem, but it was because I had padding on the input boxes. Once removed it fixed everything. Which makes me think I hate Firefox even though this was my fault. :P




回答3:


First Try to detect only firefox browser

	if (navigator.userAgent.toLowerCase().indexOf('firefox') !== -1) {
		$.fn.modal.Constructor.prototype.enforceFocus = function (){};
	}


来源:https://stackoverflow.com/questions/22050641/month-select-in-datepicker-inside-a-bootstrap-modal-wont-work-in-firefox

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