Home Web Front-end CSS Tutorial Detailed explanation of input submit, button and enter key submission data

Detailed explanation of input submit, button and enter key submission data

Dec 20, 2016 pm 02:33 PM

<form>
  <input name="name">
  <input type="submit" value="提交">
</form>
Copy after login

Submit in this way. When the input value is 22222222, the url submitted later will become localhost:3980/input.html?name=222222
There are some noteworthy details:

After setting type=submit, enter The control will become a button, and the displayed text will be its value. The default value is Submit.
The default value of form[method] is GET, so after submission, the GET method will be used to jump to the page.
The default value of input[type] is text, so the first input is displayed as a text box.

Input is actually a button modified from an input control, which originated from the crude design of the early days of the Web. We can verify this by setting a name to it:


The URL after submission will become localhost:3980/input .html?name=222222&btn=Submit

Note that the URL is /?key=foo&btn=ok. The input control as a button is also submitted to the server as a form input. Is it an interactive control or a data control? The positioning is a little unclear. In addition, its style is difficult to customize and cannot be used as a container for other tags, so it is recommended not to use input as a form submission button.

Note: The type attribute of input can also be button. At this time, it is just a button and will not trigger form submission.

2. button [tpe=submit] The semantics of button are very clear, that is, a button does not contain data, and its function is user interaction. But it also has type and value attributes. The default value of type is submit, so clicking a button will cause the form to be submitted:

<form>
 <input name=&#39;key&#39;>
 <button>确定</button>
</form>
Copy after login

If you are doing IE browser compatibility, please remember that the default value of button[type] in IE is button, this Meaning it's just a button and doesn't trigger a form submission.

In addition, we specify the text of the button by setting the element content. This means that the button is a container control, which can contain arbitrary HTML tags, and the style is easier to customize. This is one of the reasons why buttons are used extensively as examples in the Bootstrap documentation.

 However, buttons will be messy. Button can set name and value. When the form is submitted, the value will be submitted to the server as form data. In IE, the content between the button's start and end tags will even be submitted to the server as the value corresponding to the name. The similarities between button and input don't end there. Button can also be set with type=reset. Clicking the button will cause the form to be reset (this is quite useful). w3school gives the following example:

<form action="form_action.asp" method="get">
 First name: <input type="text" name="fname" />
 Last name: <input type="text" name="lname" />
 <button type="submit" value="Submit">Submit</button>
 <button type="reset" value="Reset">Reset</button>
</form>
Copy after login

Not much to say about buttons. It is recommended to use buttons as interactive buttons to submit forms. Please also pay attention to setting type=submit to be compatible with IE.

Press the Enter key to submit the form

The Enter key can submit the form! But you may have noticed that not all forms can be submitted with the Enter key. Let’s look at the HTML2.0 standard:

When there is only one single-line text input field in a form, the user agent should accept Enter in that field as a request to submit the form. When using a text input control, the user agent should accept the Enter key to submit the form.

"Single line" refers to the type being text rather than textarea. Obviously it is unacceptable to press enter to submit the form in textarea. In fact, in practice, multiple single-line inputs can also be submitted using Enter, such as the login page.

4. Prevent form submission

Preventing form submission is also a common topic, usually used for client-side form validation. The general method is to set onsubmit:

<form onsubmit="return false;">
 <input name=&#39;key&#39;>
 <input value=&#39;ok&#39; type=&#39;submit&#39;>
</form>
Copy after login
Copy after login

You only need to return false at the end of a series of onsubmit statements to prevent it from submitting. If you want to call a method to decide whether to prevent submission, remember to return the return value of the method here:

<form onsubmit="return false;">
 <input name=&#39;key&#39;>
 <input value=&#39;ok&#39; type=&#39;submit&#39;>
</form>
Copy after login
Copy after login

The above is all, thank you for supporting PHP Chinese website

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1252
29
C# Tutorial
1226
24
Google Fonts   Variable Fonts Google Fonts Variable Fonts Apr 09, 2025 am 10:42 AM

I see Google Fonts rolled out a new design (Tweet). Compared to the last big redesign, this feels much more iterative. I can barely tell the difference

How to Create an Animated Countdown Timer With HTML, CSS and JavaScript How to Create an Animated Countdown Timer With HTML, CSS and JavaScript Apr 11, 2025 am 11:29 AM

Have you ever needed a countdown timer on a project? For something like that, it might be natural to reach for a plugin, but it’s actually a lot more

HTML Data Attributes Guide HTML Data Attributes Guide Apr 11, 2025 am 11:50 AM

Everything you ever wanted to know about data attributes in HTML, CSS, and JavaScript.

How to select a child element with the first class name item through CSS? How to select a child element with the first class name item through CSS? Apr 05, 2025 pm 11:24 PM

When the number of elements is not fixed, how to select the first child element of the specified class name through CSS. When processing HTML structure, you often encounter different elements...

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

A Proof of Concept for Making Sass Faster A Proof of Concept for Making Sass Faster Apr 16, 2025 am 10:38 AM

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

How We Created a Static Site That Generates Tartan Patterns in SVG How We Created a Static Site That Generates Tartan Patterns in SVG Apr 09, 2025 am 11:29 AM

Tartan is a patterned cloth that’s typically associated with Scotland, particularly their fashionable kilts. On tartanify.com, we gathered over 5,000 tartan

See all articles