Press enter form to submit the problem automatically. Unexpected discovery

Time:2020-10-23

Copy code

The code is as follows:

<!DOCTYPE html>
<html>
<head>
<meta charset=”utf-8″>
<title></title>
</head>
<body>
<form action=”http://www.baidu.com”>
<input type=”text” name=”name”/>
<input type=”button” onclick=”j avascript:void (0) “value = submit” >
</form>
</body>
</html>

For the above form, press enter in the name input box, and the form will be submitted. And there is no submit button in the form.

After a lot of searching, we finally found out why: if there is only one text box in the form, when you press enter, the form will be submitted.

So the solution is easy, add a useless text box and hide it.

Copy code

The code is as follows:

<input type=”text” style=’display:none’ />

Note: you can’t add a hidden box.

Copy code

The code is as follows:

<input type=”hidden” />