Autofocus input in twitter bootstrap modal

后端 未结 6 968
囚心锁ツ
囚心锁ツ 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:14

    I'm using Bootstrap 3.0 (hopefully, this works with 3.1 as well).

    We had to use tabindex="-1" because that allows the ESC key to close the modal.

    So I was able to fix this issue using:

    // Every time a modal is shown, if it has an autofocus element, focus on it.
    $('.modal').on('shown.bs.modal', function() {
      $(this).find('[autofocus]').focus();
    });
    

提交回复
热议问题