The difference between button and input in a form
Mar 19, 2018 am 09:23 AMThis 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 button1 , 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<input> tag specifies an input field in which users can enter dataThe <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
<form action="#">
<input type="text" name="username"/><br/>
<input type="password" name="password"/><br/>
<input type="submit" value="登录"/></form>
Copy after login If you want to check the form data before submitting the data:
<form action="#"> <input type="text" name="username"/><br/> <input type="password" name="password"/><br/> <input type="submit" value="登录"/></form>
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.<form action="#" onsubmit="return check()">
用户名:<input type="text" name="username"/><br/>
密码:<input type="password" name="password"/><br/>
<input type="submit" value="登录"/>
<input type="button" value="提醒" onclick="remind()"/></form><script LANGUAGE="JavaScript">function check(){
console.log("提交前先验证"); var checkElement=document.getElementsByTagName("input"); if(checkElement[0].value==="" || checkElement[1].value==="") { return false;//当用户名或者密码为空时返回false,此时表单不会提交 }
}function remind(){
alert("这是一个简单按钮,默认不会提交表单数据,不会刷新页面");
}</script>
Copy after login
三、<form action="#" onsubmit="return check()"> 用户名:<input type="text" name="username"/><br/> 密码:<input type="password" name="password"/><br/> <input type="submit" value="登录"/> <input type="button" value="提醒" onclick="remind()"/></form><script LANGUAGE="JavaScript">function check(){ console.log("提交前先验证"); var checkElement=document.getElementsByTagName("input"); if(checkElement[0].value==="" || checkElement[1].value==="") { return false;//当用户名或者密码为空时返回false,此时表单不会提交 } }function remind(){ alert("这是一个简单按钮,默认不会提交表单数据,不会刷新页面"); }</script>
<button type="submit"></button>表单数据提交按钮,与<input type="submit"/> 用法相同
Copy after login
四、<button type="submit"></button>表单数据提交按钮,与<input type="submit"/> 用法相同
<button type="button"></button>Normal buttons are used the same as <input type="button"/><form action="#" onsubmit="return check()">
用户名:<input type="text" name="username"/><br/>
密码:<input type="password" name="password"/><br/>
<button type="submit">登录</button>
<button type="button"onclick="remind()">提醒</button></form><script LANGUAGE="JavaScript">function check(){
console.log("提交前先验证"); var checkElement=document.getElementsByTagName("input"); if(checkElement[0].value==="" || checkElement[1].value==="") { return false;//当用户名或者密码为空时返回false,此时表单不会提交 }
}function remind(){
alert("这是一个简单按钮,默认不会提交表单数据,不会刷新页面");
}</script>
Copy after login
Note: <form action="#" onsubmit="return check()"> 用户名:<input type="text" name="username"/><br/> 密码:<input type="password" name="password"/><br/> <button type="submit">登录</button> <button type="button"onclick="remind()">提醒</button></form><script LANGUAGE="JavaScript">function check(){ console.log("提交前先验证"); var checkElement=document.getElementsByTagName("input"); if(checkElement[0].value==="" || checkElement[1].value==="") { return false;//当用户名或者密码为空时返回false,此时表单不会提交 } }function remind(){ alert("这是一个简单按钮,默认不会提交表单数据,不会刷新页面"); }</script>
##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> ; 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
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!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

deepseek What is the difference between r1 and v3 version

Does Bitcoin have stocks? Does Bitcoin have equity?

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?

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

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?
