Autofocus input in twitter bootstrap modal

后端 未结 6 919
囚心锁ツ
囚心锁ツ 2020-12-03 09:52

I have such problem - I need to autofocus some element inside twitter bootstrap modal (after it shows). The tricky part is here - content of this modal is loaded using \'dat

6条回答
  •  离开以前
    2020-12-03 10:01

    You have an input with the autofocus attribute you want focused when the bootstrap modal is shown.

    Is your modal markup available when JavaScript is loaded?

    Register an event handler on all .modal elements for the shown.bs.modal event

    $('.modal').on('shown.bs.modal', function() {
      $(this).find('[autofocus]').focus();
    });
    

    Is your modal markup dynamically generated?

    Register an event handler on the entire document for the shown.bs.modal event.

    $(document).on('shown.bs.modal', '.modal', function () {
        $(this).find('[autofocus]').focus();
    });
    

提交回复
热议问题