我试图在模态显示后将焦点设置在输入上。
到目前为止我看到并尝试过的解决方案和链接:How to focus on a form input text field on page load using jQuery? , Jquery Focus on input field , 但它们都不起作用。
我的代码其实很简单:
$input = $("#letter-change-text").find("input[type='text']");
$input.val($.trim("I am just testing!!"));
$("#letter-change-text").modal('show');
$input.focus(); // this should focus the textbox in order to allow immediately input by the user
您可以在 JSfiddle 中查看此问题.
我也试过没有成功:
setTimeout(function(){
$input.focus();
}, 0);
最佳答案
使用显示的事件
$(function(){
$input = $("#letter-change-text").find("input[type='text']");
$input.val($.trim("I am just testing!!"));
$('#letter-change-text').on('shown.bs.modal', function (e) {
$input.focus();
}).modal('show');
});
关于javascript - 如何在模态中将焦点设置为输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26021493/