按钮类型“按钮”与“提交”

2024-04-28 09:24:51 发布

您现在位置:Python中文网/ 问答频道 /正文

type="button"buttontype="submit"之间有区别吗?是功能上的差异,还是只是一个描述性的名称,以便更容易地阅读代码?

这和input不同吗?


Tags: 代码功能名称inputtypebutton差异submit
3条回答

按钮的样式化比用于锚定标记(链接)的输入要好得多。

  • 图像
  • 内容等

输入可以实现与按钮相同的功能,但设计更丑。

假设输入是老式的,按钮更酷。

类型为“button”的按钮不会提交表单,但类型为“type”或类型为“submit”(默认)的按钮将提交表单。type=submit的按钮与type=submit的输入几乎相同,但按钮可以包含HTML内容。

来自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.

至于buttoninput之间的区别:

  • 一个button可以有一个单独的值作为数据,而对于一个input来说,数据和按钮文本总是相同的:

    <input type="button" value="Button Text"> <!-- Form data will be "Button Text" -->
    <button type="button" value="Data">Button Text</button>
    
  • button可以有HTML内容(例如图像),而input只能有文本。

  • 与CSS中的其他控件(如文本字段)相比,button可能更容易区分。注意浏览器的向后兼容性。

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

相关问题 更多 >