显示模态后将焦点设置为控制
Set focus to control after show modal
我需要在显示模态事件后将焦点设置在模态内的控件上。
这是我目前的代码:
$('#searcherCustomerModal').on('shown', function () {
$("#txtNameCustomer").select();
});
但是不行。有什么想法吗?
在 jQuery 中,您使用 .focus()
not .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">×</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>
我需要在显示模态事件后将焦点设置在模态内的控件上。
这是我目前的代码:
$('#searcherCustomerModal').on('shown', function () {
$("#txtNameCustomer").select();
});
但是不行。有什么想法吗?
在 jQuery 中,您使用 .focus()
not .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">×</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>