Bootbox.prompt()将重点放在文本字段上 [英] Bootbox.prompt() set focus on text field

查看:649
本文介绍了Bootbox.prompt()将重点放在文本字段上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法自动将焦点设置在Bootbox的文本字段上.

I can't seem to set the focus on Bootbox's textfield automatically.

这是我的代码: 函数setCode(myCode){

here's my code: function setCode(myCode){

    bootbox.confirm("Enter your PIN: <input id='pin_code' type='password' name='pin_code'></input>", 
            function(result) {
                if(result)
                console.log($("#pin_code").val());
            });
        $("#pin_code").focus();
    };

也许有关bootbox的代码有些阻碍?

Perhaps something about bootbox's code is getting in the way?

推荐答案

您可以注册设置焦点的shown事件处理程序(如@Shiny所建议).但是,从Bootstrap 3和Bootbox v4.x.x开始,显示的事件已命名为名称空间,有必要使用完全限定的名称shown.bs.modal来注册事件处理程序.因此代码将是:

You can register a shown event handler that sets the focus (as suggested by @Shiny). However, as of Bootstrap 3 and Bootbox v4.x.x, the shown event is namespaced and it is necessary to use the fully qualified name shown.bs.modal to register the event handler. So the code would be:

var box = bootbox.confirm("Enter your PIN: <input id='pin_code' type='password' name='pin_code'></input>", 
    function(result) {
        if(result)
        console.log($("#pin_code").val());
    });

box.on('shown.bs.modal',function(){
  $("#pin_code").focus();
});

尝试按原样设置焦点不起作用,因为在调用bootbox.confirm后立即不显示对话框.不可见的元素不能具有焦点.

Trying to set the focus as you were doesn't work because the dialog isn't visible immediately after the call to bootbox.confirm. Elements that aren't visible can't have focus.

这篇关于Bootbox.prompt()将重点放在文本字段上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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