How do I get bootstrap-datepicker to work with Bootstrap 3?

后端 未结 2 407
北海茫月
北海茫月 2020-12-12 15:43

I use the version of bootstrap-datepicker maintained by eternicode (Andrew Rowls).

On Bootstrap 2 it worked, but now it doesn\'t work with the Bootstrap 3 library.

2条回答
  •  攒了一身酷
    2020-12-12 16:18

    For anyone else who runs into this...

    Version 1.2.0 of this plugin (current as of this post) doesn't quite work in all cases as documented with Bootstrap 3.0, but it does with a minor workaround.

    Specifically, if using an input with icon, the HTML markup is of course slightly different as class names have changed:

    It seems because of this, you need to use a selector that points directly to the input element itself NOT the parent container (which is what the auto generated HTML on the demo page suggests).

    $('*[data-datepicker="true"] input[type="text"]').datepicker({
        todayBtn: true,
        orientation: "top left",
        autoclose: true,
        todayHighlight: true
    });
    

    Having done this you will probably also want to add a listener for clicking/tapping on the icon so it sets focus on the text input when clicked (which is the behaviour when using this plugin with TB 2.x by default).

    $(document).on('touch click', '*[data-datepicker="true"] .input-group-addon', function(e){
        $('input[type="text"]', $(this).parent()).focus();
    });
    

    NB: I just use a data-datepicker boolean attribute because the class name 'datepicker' is reserved by the plugin and I already use 'date' for styling elements.

提交回复
热议问题