专注于模态打开的输入字段

Focus on Input Field on Modal Open

我有一个在页面加载时打开的模式。我需要将焦点设置在文本框上。我尝试使用我搜索过的几个代码,但它不起作用。这是我的 js:

$(document).ready(function(){
        $("#myModal").modal('show', function() {
        $('#keyword', this).focus();
    });

});

我的输入框是这个

<input type="text" style="width: 50%; margin-left:2px; " id="keyword"               class="input-group inline form-control search" name="keyword" placeholder="Search" ng-model="keyword" ng-required="true" required >

可能是什么问题?

很简单,把.on("show"改成.on("shown.bs.modal"就可以了:D

在此处查看有关 bootstrap 模态事件的更多参考:http://getbootstrap.com/javascript/#modals-events

编辑:

这应该可以如您所愿::D 抱歉,我没有仔细阅读您的问题...

$(document).ready(function() {

    //to have your input focused every your modal open
    $('#myModal').on("shown.bs.modal", function() {
        $('#keyword').focus();
    });

    //to open the modal when document is ready
    $("#myModal").modal('show');
});

加法:

如果你想关注第一个 .form-control 里面的模态:

$(document).ready(function() {
    $('#myModal').on("shown.bs.modal", function() {
        $(this).find(".form-control:first").focus();
    });
    $("#myModal").modal('show');
});