javascript - Bootbox.prompt() set focus on text field - Stack Overflow

I can't seem to set the focus on Bootbox's textfield automatically.here's my code:functi

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

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

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

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

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

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

Share Improve this question asked Feb 9, 2014 at 9:33 BrownChiLDBrownChiLD 3,73310 gold badges47 silver badges64 bronze badges 1
  • 1 maybe you should accept correct answer... – Legionar Commented Oct 28, 2015 at 10:22
Add a ment  | 

3 Answers 3

Reset to default 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.

You can try this code

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

I know that this is old but check that if you're using Chrome, you don't have Dev Tools open.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742276883a4413754.html

相关推荐

  • javascript - Bootbox.prompt() set focus on text field - Stack Overflow

    I can't seem to set the focus on Bootbox's textfield automatically.here's my code:functi

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信