How to focus on a form input text field on page load using jQuery?

前端 未结 11 1174
天命终不由人
天命终不由人 2020-11-28 18:56

This is probably very simple, but could somebody tell me how to get the cursor blinking on a text box on page load?

相关标签:
11条回答
  • 2020-11-28 19:26

    place after input

    <script type="text/javascript">document.formname.inputname.focus();</script>
    
    0 讨论(0)
  • 2020-11-28 19:32

    Sure:

    <head>
        <script src="jquery-1.3.2.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(function() {
                $("#myTextBox").focus();
            });
        </script>
    </head>
    <body>
        <input type="text" id="myTextBox">
    </body>
    
    0 讨论(0)
  • 2020-11-28 19:32

    HTML:

      <input id="search" size="10" />
    

    jQuery:

    $("#search").focus();
    
    0 讨论(0)
  • 2020-11-28 19:33

    The Simple and easiest way to achieve this

    $('#button').on('click', function () {
        $('.form-group input[type="text"]').attr('autofocus', 'true');
    });
    
    0 讨论(0)
  • 2020-11-28 19:35

    You can use HTML5 autofocus for this. You don't need jQuery or other JavaScript.

    <input type="text" name="some_field" autofocus>
    

    Note this will not work on IE9 and lower.

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