Bootstrap 自动对焦在模态引导中不起作用

fv2wmkja  于 2022-12-16  发布在  Bootstrap
关注(0)|答案(3)|浏览(166)

自动对焦仅在下次启动模型时加载页面时才起作用,自动对焦在输入类型中不起作用

<div class="modal fade" id="OrganModal" role="dialog">
  <div class="modal-dialog mainModal">
    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Organ List</h4>
      </div>
      <div class="modal-body">
        <div class="divSearch orgsearch" tabindex="1">
          <div class="SearchBox">
            <input type="text" onkeyup="OrganSearch()" id="btnTextOrgan" placeholder="Search" autofocus="autofocus" />
          </div>
          <div class="SearchAction">
            <div class="SearchActionAlign">
              <a href="#" onclick="clearSearchResultOfOrgan()" class="NonAction ActionClick">
                <img src="../../assets/images/icons/close.png" alt="Cancel" class="ImgClose" />
              </a>
            </div>
          </div>
        </div>
        <div id="organList" class="organ_list"></div>
      </div>
    </div>
  </div>
</div>
3zwtqj6y

3zwtqj6y1#

由于bootstrap是在去掉autofocus的模态上应用tabindex=-1的......所以尝试使用bootstrap模态的shown.bs.modal方法

堆栈代码段

$('.modal').on('shown.bs.modal', function() {
  $(this).find('[autofocus]').focus();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        <input type="text" class="form-control">
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>
mbjcgjjk

mbjcgjjk2#

之后在modal中将autofocus属性添加到input
在脚本中添加

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

rekjcdws3#

var myModal = document.getElementById('myModal')
var myInput = document.getElementById('myInput')

myModal.addEventListener('shown.bs.modal', function () {
  myInput.focus()
})

相关问题