自动对焦不适用于模态引导程序

时间:2018-02-16 08:44:44

标签: jquery twitter-bootstrap

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

<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>

1 个答案:

答案 0 :(得分:2)

因为bootstrap在删除tabindex=-1的模态上应用autofocus所以尝试使用shown.bs.modal bootstrap模式的方法

Stack Snippet

&#13;
&#13;
$('.modal').on('shown.bs.modal', function() {
  $(this).find('[autofocus]').focus();
});
&#13;
<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>
&#13;
&#13;
&#13;