【问题标题】:Set focus to control after show modal显示模态后将焦点设置为控制
【发布时间】:2015-04-26 11:03:09
【问题描述】:

在显示模式事件之后,我需要将焦点设置在模式内的控件上。

这是我目前的代码:

$('#searcherCustomerModal').on('shown', function () {
    $("#txtNameCustomer").select();
});

但它不起作用。有什么想法吗?

【问题讨论】:

  • 为什么会有knockout.js标签?
  • 我正在使用 knockout.js 进行开发...我将删除它...
  • 已修复,我在这个问题中找到了解决方案!! :link .. $('#myModal').on('shown.bs.modal', function () { $('#textareaID').focus(); })
  • @FreddyCastelblancoMacias 您应该发布您的答案并接受它,以便未来的用户可以看到。

标签: jquery twitter-bootstrap bootstrap-modal


【解决方案1】:

在 jQuery 中,您使用 .focus() 而不是 .select() 设置焦点
此外,在 Bootstrap 3 中,显示的事件称为 shown.bs.modal

所以代码应该是这样的:

$('#searcherCustomerModal').on('shown.bs.modal', function () {
    $("#txtNameCustomer").focus();
});

堆栈片段中的演示

$('#searcherCustomerModal').on('shown.bs.modal', function () {
    $("#txtNameCustomer").focus();
});
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/css/bootstrap.css" rel="stylesheet"/>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/js/bootstrap.js"></script>

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#searcherCustomerModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="searcherCustomerModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <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" id="txtNameCustomer" 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-09
    • 2013-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多