How to Set focus to first text input in a bootstrap modal after shown

后端 未结 16 1178
感情败类
感情败类 2020-12-07 15:12

I load a dynamic bootstrap modal and it contains few text inputs. The issue i face that i want the cursor to focus on the first input in this modal, and this is not happenin

16条回答
  •  忘掉有多难
    2020-12-07 16:08

    I think the most correct answer, assuming the use of jQuery, is a consolidation of aspects of all the answers in this page, plus the use of the event that Bootstrap passes:

    $(document).on('shown.bs.modal', function(e) {
      $('input:visible:enabled:first', e.target).focus();
    });
    

    It also would work changing $(document) to $('.modal') or to add a class to the modal that signals that this focus should occur, like $('.modal.focus-on-first-input')

提交回复
热议问题