简体   繁体   中英

JS: prevent text input field from gaining focus unless clicked

When I use Chrome on Android to fill a web form, by pressing the blue "enter" button the browser auto focuses on the next text field. How can I prevent this from happening?

 <form action="/action_page.php" method="get"> <input name="text1" value="Bla bla"><br> <input name="text2" value="Some text"><br> <input type="submit" value="Submit"> </form> 

If anyone is curious why I need this, see here (the answer to that question does not solve my actual problem even though it solved the toy version of it).

To disable autofocus on all input elements use this.

$(function(){
        $('input').blur();
    });

To disable autofocus on a particular element, pass the element id as below:

$(function(){
    $('input#someid').blur();
});

OR you can use this

$('input').each(function() {               
    $(this).attr('tabindex', '-1');
});

Simply

<form action="/action_page.php" method="get">
  <input name="text1" value="Bla bla" tabindex="-1"><br>
  <input name="text2" value="Some text" tabindex="-1"><br>
  <input type="submit" value="Submit">
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM