[英]Focus input on modal show
How can I auto focus an input on modal show? 如何自动将输入重点放在模态显示上? Currently I do this but it isn't working: 目前我这样做但它不起作用:
jQuery(document).ready(function($) {
$('#myModal').on('show.bs.modal', function() {
$('input[name="myInput"]').focus();
});
});
http://jsfiddle.net/1aeur58f/2513/ http://jsfiddle.net/1aeur58f/2513/
Your code is basically correct. 你的代码基本上是正确的。 But the event show.bs.modal
is triggerede before the modal has been shown. 但事件show.bs.modal
在模态显示之前是triggerede。 You need to use shown.bs.modal
event instead. 您需要使用shown.bs.modal
事件。
jQuery(function($) {
$('#myModal').on('shown.bs.modal', function() {
$('input[name="myInput"]').focus();
});
});
http://jsfiddle.net/1aeur58f/2544/ http://jsfiddle.net/1aeur58f/2544/
在您的代码中,使用autofocus
<input name="myInput" value="MyVal" type="text" autofocus/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.