如何设置模式输入的重点? [英] How to set focus on input in modal?

查看:54
本文介绍了如何设置模式输入的重点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在模态显示之后,我试图将重点放在输入上.

I'm trying to set focus on an input after the modal shows.

到目前为止,我已经看过并尝试过的解决方案和链接: jQuery专注于输入字段,但没有一个起作用.

The solutions and links I've seen and tried so far: How to focus on a form input text field on page load using jQuery?, Jquery Focus on input field, and none of them works.

我的代码实际上非常简单:

My code is actually pretty simple:

$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 中检查此问题.

You can check this issue in JSfiddle.

我也尝试过但没有成功:

I also have tried without any success:

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');
});

这篇关于如何设置模式输入的重点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆