Button type “button” vs. “submit”

故事扮演 提交于 2019-11-27 05:08:22

From MDN:

type
The type of the button. Possible values are:

  • submit: The button submits the form data to the server. This is the default if the attribute is not specified, or if the attribute is dynamically changed to an empty or invalid value.
  • reset: The button resets all the controls to their initial values.
  • button: The button has no default behavior. It can have client-side scripts associated with the element's events, which are triggered when the events occur.

As for the difference between button and input:

  • A button can have a separate value as data, while for an input the data and button text are always the same:

    <input type="button" value="Button Text"> <!-- Form data will be "Button Text" -->
    <button type="button" value="Data">Button Text</button>
    
  • A button can have HTML content (e.g. images), while an input can only have text.

  • A button may be easier to tell apart from other input controls (like text fields) in CSS. Note backwards browser compatibility.

    input {
    
    }
    button { /* Always works */
    
    }
    input[type=button] { /* Not supported in IE < 7 */
    
    }
    

A button with type "button" won't submit a form but one with no type or type=submit (the default) will. Buttons with type=submit are nearly the same as inputs with type=submit but buttons are able to contain HTML content.

They have different default behaviour regarding submitting form data to the server The button has an attribute named "type" and can contain those values:

submit: Has default behaviour of submitting the form data to the server. This is the default if the attribute is not specified, or if the attribute is dynamically changed to an empty or invalid value.

button: The button has no default behavior. It can have client-side scripts associated with the element's events, which are triggered when the events occur.

<button type="button"></button> buttons will not submit a form - they don't do anything by default. Button won't submit form on its own.It is a simple button which is used to perform some operation by using javascript whereas Submit is a kind of button which by default submit the form whenever user clicks on submit button.

Buttons can be stylized much better than inputs can be used for anchor tags(links).

  • Images
  • Content etc.

Inputs can achieve the same functionality as buttons but uglier design.

Let's say inputs are oldschool, buttons are cooler.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!