Twitter bootstrap modal input field focus

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

I have the following modal form from example:




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

    You can do: e.g

    <%= f.text_field :first_name, class: "form-control", placeholder: "Enter first name", autofocus: true%>
    

    just add this: autofocus: true

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

    I think the updated answer is pretty clever. Here is another way to solve it.

    Bootstrap 3.2's js file includes a script to manage focus during and after the modal's fade transition. This interferes with any jQuery, javascript or rails+HTML5 focusing on a form field in the modal - bootstrap removes your focus after the modal loads.

    To remove bootstrap's ability to override your focus, comment out this line in the Modal script area:

    transition ?
        that.$element.find('.modal-dialog') // wait for modal to slide in
          .one($.support.transition.end, function () {
            // that.$element.focus().trigger(e)
            // the line above is stealing your focus after modal loads!
          })
          .emulateTransitionEnd(300) :
        that.$element.focus().trigger(e) 
    

    Now your field should be able to have focus in any modal form.

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

    For a general solution that doesn't require specific code for each dialog, you can use this:

    $('.modal').on('shown.bs.modal', function () {
      $(this).find('input:text:visible:first').focus();
    })
    
    0 讨论(0)
提交回复
热议问题