我需要在show modal事件之后将焦点设置在模态内的控件上。
这是我到目前为止的代码:
$('#searcherCustomerModal').on('shown', function () {
$("#txtNameCustomer").select();
});
但它不起作用。任何想法?
答案 0 :(得分:3)
在jQuery中,您将焦点设为.focus()
而不是.select()
此外,在Bootstrap 3中,显示的事件称为shown.bs.modal
所以代码应如下所示:
$('#searcherCustomerModal').on('shown.bs.modal', function () {
$("#txtNameCustomer").focus();
});
Stack Snippets中的演示:
$('#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">×</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>