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

后端 未结 16 1123
感情败类
感情败类 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 15:54

    this is the most general solution

    $('body').on('shown.bs.modal', '.modal', function () {
        $(this).find(":input:not(:button):visible:enabled:not([readonly]):first").focus();
    });
    
    • works with modals added to DOM after page load
    • works with input, textarea, select and not with button
    • ommits hidden, disabled, readonly
    • works with faded modals, no need for setInterval

提交回复
热议问题