Table of Contents
<button type="submit"></button>表单数据提交按钮,与<input type="submit"/> 用法相同
Copy after login
" >
<button type="submit"></button>表单数据提交按钮,与<input type="submit"/> 用法相同
Copy after login
Home Web Front-end JS Tutorial The difference between button and input in a form

The difference between button and input in a form

Mar 19, 2018 am 09:23 AM
button input the difference

This time I will bring you the difference between button and input in the form. What are the precautions for using button and input in the form? The following is a practical case, let's take a look.

Let’s talk about the definitions of button and input first:

##<button> The label defines a button

1 , inside the <button> element you can place any content, such as text or images. This is the difference between this element and the button created using the <input> element;

2. The <button> control provides more powerful functions and richer content;

3. All content between the <button> and </button> tags is the content of the button, including any acceptable body content, such as text or

multimedia

content.

<input> tag specifies an input field in which users can enter data

The <input> element is used within the <form> element to Declare an input control that allows users to enter data; the specific input type depends on the type attribute

The following four types of buttons are described in detail:

<input type="submit"/>, <input type= "button"/>, <button type="submit"></button>, <button type="button"></button>

1. <input type="submit"/>: When the user clicks this button, the form will send the form content according to the action attribute setting of the <form> tag. When clicked, the page will refresh

&lt;form action=&quot;#&quot;&gt;
    &lt;input type=&quot;text&quot; name=&quot;username&quot;/&gt;&lt;br/&gt;
    &lt;input type=&quot;password&quot; name=&quot;password&quot;/&gt;&lt;br/&gt;
    &lt;input type=&quot;submit&quot; value=&quot;登录&quot;/&gt;&lt;/form&gt;
Copy after login
If you want to check the form data before submitting the data:

Returning false in the check function will prevent the default behavior of submit, that is, Prevent form data submission (prevent page refresh)

Note:

onsubmit=

"return check()" Medium The return cannot be omitted

2.

<input type="button"/>Ordinary buttons must be paired with JS to be useful, such asonclickEvents etc.
&lt;form action=&quot;#&quot; onsubmit=&quot;return check()&quot;&gt;
    用户名:&lt;input type=&quot;text&quot; name=&quot;username&quot;/&gt;&lt;br/&gt;
    密码:&lt;input type=&quot;password&quot; name=&quot;password&quot;/&gt;&lt;br/&gt;
    &lt;input type=&quot;submit&quot; value=&quot;登录&quot;/&gt;
    &lt;input type=&quot;button&quot; value=&quot;提醒&quot; onclick=&quot;remind()&quot;/&gt;&lt;/form&gt;&lt;script  LANGUAGE=&quot;JavaScript&quot;&gt;function check(){
    console.log("提交前先验证");    var checkElement=document.getElementsByTagName("input");     if(checkElement[0].value==="" || checkElement[1].value==="") {         return false;//当用户名或者密码为空时返回false,此时表单不会提交     }
}function remind(){
    alert("这是一个简单按钮,默认不会提交表单数据,不会刷新页面");
}&lt;/script&gt;
Copy after login

三、

&lt;button type=&quot;submit&quot;&gt;&lt;/button&gt;表单数据提交按钮,与&lt;input type=&quot;submit&quot;/&gt; 用法相同
Copy after login

四、

<button type="button"></button>Normal buttons are used the same as <input type="button"/>
&lt;form action=&quot;#&quot; onsubmit=&quot;return check()&quot;&gt;
    用户名:&lt;input type=&quot;text&quot; name=&quot;username&quot;/&gt;&lt;br/&gt;
    密码:&lt;input type=&quot;password&quot; name=&quot;password&quot;/&gt;&lt;br/&gt;
    &lt;button type=&quot;submit&quot;&gt;登录&lt;/button&gt;
    &lt;button type=&quot;button&quot;onclick=&quot;remind()&quot;&gt;提醒&lt;/button&gt;&lt;/form&gt;&lt;script  LANGUAGE=&quot;JavaScript&quot;&gt;function check(){
    console.log("提交前先验证");    var checkElement=document.getElementsByTagName("input");     if(checkElement[0].value==="" || checkElement[1].value==="") {         return false;//当用户名或者密码为空时返回false,此时表单不会提交     }
}function remind(){
    alert("这是一个简单按钮,默认不会提交表单数据,不会刷新页面");
}&lt;/script&gt;
Copy after login

Note:

##When <button> is not in the <form> form, the browser’s default type is button;

When <button> is in a <form> form, different browsers use different default values ​​for the type attribute of the <button> element;

Therefore, it is recommended to always set the type value for the button.

To summarize:

    ##<button type="submit"></button&gt ; Same usage as <input type="submit"/>, used as form data submission button, the page will be refreshed by default;
  • ## <button type="button"></button> and

    <input type="button"/>
  • have the same usage. They are both ordinary buttons and the page will not be refreshed by default. .
  • # I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

    Recommended reading:

What are the steps for jest to test react native components

Detailed explanation of implicit calls in javascript

JS adds new element node

The above is the detailed content of The difference between button and input in a form. For more information, please follow other related articles on the 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 Article Tags

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)

deepseek What is the difference between r1 and v3 version deepseek What is the difference between r1 and v3 version Feb 19, 2025 pm 03:24 PM

deepseek What is the difference between r1 and v3 version

Does Bitcoin have stocks? Does Bitcoin have equity? Does Bitcoin have stocks? Does Bitcoin have equity? Mar 03, 2025 pm 06:42 PM

Does Bitcoin have stocks? Does Bitcoin have equity?

Summary of FAQs for DeepSeek usage Summary of FAQs for DeepSeek usage Feb 19, 2025 pm 03:45 PM

Summary of FAQs for DeepSeek usage

What is the difference between pre-market and after-market trading? Detailed explanation of the differences between pre-market and after-market trading What is the difference between pre-market and after-market trading? Detailed explanation of the differences between pre-market and after-market trading Mar 03, 2025 pm 11:54 PM

What is the difference between pre-market and after-market trading? Detailed explanation of the differences between pre-market and after-market trading

Why is Bittensor said to be the 'bitcoin' in the AI ​​track? Why is Bittensor said to be the 'bitcoin' in the AI ​​track? Mar 04, 2025 pm 04:06 PM

Why is Bittensor said to be the 'bitcoin' in the AI ​​track?

Is there any difference between South Korean Bitcoin and domestic Bitcoin? Is there any difference between South Korean Bitcoin and domestic Bitcoin? Mar 05, 2025 pm 06:51 PM

Is there any difference between South Korean Bitcoin and domestic Bitcoin?

Vertical proxy: Application scenarios and interpretation of disruptive potential of encryption native proxy Vertical proxy: Application scenarios and interpretation of disruptive potential of encryption native proxy Mar 04, 2025 am 10:21 AM

Vertical proxy: Application scenarios and interpretation of disruptive potential of encryption native proxy

Pepe bought and sold out in a big way, is MUTM a smarter investment in 2025? Pepe bought and sold out in a big way, is MUTM a smarter investment in 2025? Mar 03, 2025 pm 07:09 PM

Pepe bought and sold out in a big way, is MUTM a smarter investment in 2025?

See all articles