Twitter bootstrap modal input field focus

后端 未结 9 711
不知归路
不知归路 2020-12-08 03:56

I have the following modal form from example:




        
相关标签:
9条回答
  • 2020-12-08 04:17

    I think the shown event name changed in Bootstrap 3, as explained in this post.

    As @MrDBA notes, in Bootstrap 3 the event name is changed to shown.bs.modal.

    So, for Bootstrap 3 use:

    $('#myModal').on('shown.bs.modal', function () {
        $('#myInput').focus();
    })
    
    0 讨论(0)
  • 2020-12-08 04:28

    If you have problems with "shown.bs.modal", just set a timeout.

    setTimeout(function() {
    $('#myInput').focus();
    }, 500);
    
    0 讨论(0)
  • 2020-12-08 04:32

    Just changing $(this).find("[autofocus]:first").focus(); to $(this).find("#myInput").focus(); made it work for me. If you have changed the Bootstrap API and want to undo that change, you can always just re-download and replace the file.

    0 讨论(0)
  • 2020-12-08 04:32

    You can also try

    $('#the-modal').on('shown.bs.modal', function(e) {
        $('#the-input').focus();
    });
    
    0 讨论(0)
  • 2020-12-08 04:34

    Try removing tabindex="-1" and it works nice.

    So change this:

    <div class="modal fade" id="modalID" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    

    To this:

    <div class="modal fade" id="modalID" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    
    0 讨论(0)
  • 2020-12-08 04:37

    I removed tabindex="-1" and it works so nice.

    My HTML code before changes:

    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    

    My HTML code after changes:

    <div class="modal fade" id="myModal" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    

    My Input code:

    <input id="tblModalNombreConductor" type="text" maxlength="50" placeholder="Ej: Armando Casas" autofocus/>
    

    And I have no configurations in my Javascript code.

    0 讨论(0)
提交回复
热议问题