2

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

这是我的代码:函数 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 的代码的某些东西妨碍了?

4

3 回答 3

5

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

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.

于 2015-03-03T22:22:00.230 回答
1

你可以试试这段代码

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',function(){
    $("#pin_code").focus();
});
于 2014-03-19T06:49:15.943 回答
0

我知道这很旧,但请检查如果您使用的是 Chrome,您没有打开开发工具。

于 2020-02-11T03:42:17.393 回答