Bootbox的警告框焦点无法返回输入框

3

在我的当前项目中,我使用了bootbox来替换我的旧的普通提示框和确认框,变得更加时尚。但问题是,在关闭或取消bootbox后,焦点没有返回到我的输入框。

    bootbox.alert("Please create a milestone");
    $('input[name="mile_stone[]"]:first').focus();
    return false;

除了焦点功能外,一切都按预期工作。有没有办法解决这个问题?


你能提供已完成工作的 JS Fiddle 代码吗? - DevangKantharia
2个回答

5

3
创建一个通用函数,避免重复编写聚焦代码。
这个例子是针对bootbox alert的,对于其他的(confirm等),只需调整代码即可:
<script>
$(function(){
    $('#btnAlerta').on('click',function(){
        falert('modal title','message you want to view','#nome');
    });
});
function falert(tit,msg,obj){
    var box = bootbox.alert({
        size: 'small',
        title: tit,
        message:'<p>'+msg+'</p>'
    });
    box.on('hidden.bs.modal',function(){
        $(obj).focus();
    });
}
</script>

<input type="text" id="nome" name="nome" />
<button type="button" id="btnAlerta">Alert</button>

跟随实例进行操作 https://jsfiddle.net/marsites/tLwo253e/


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接