<input type=”submit”>和<button type=”submit”>有什么不同?
时间: 2023-02-25 12:14:38 浏览: 72
<input type="submit"> 和 <button type="submit"> 的不同在于它们的外观和用途。
<input type="submit"> 是一个输入字段,通常是一个单行文本框,用于向服务器提交表单数据。它通常是一个简单的按钮,并且用户可以单击它来提交表单。
<button type="submit"> 则是一个按钮元素,它允许开发人员在其中定义更丰富的内容,如文本、图像和样式。这使得它更适合用于创建复杂的设计和交互,并且可以在提交表单时使用。
因此,当你需要简单的提交按钮时,使用 <input type="submit"> 可能是更好的选择;而当你需要创建复杂的提交按钮时,使用 <button type="submit"> 可能是更好的选择。
相关问题
简述使用<input type="button">创建按钮与使用<input type="submit">创建标签的区别
使用<input type="button">创建按钮与使用<input type="submit">创建标签的区别主要在于它们的用途和行为。
<input type="button">用于创建一个按钮,用户可以点击它来触发一些自定义的JavaScript代码,例如验证表单、提交表单等。按钮不会自动提交表单,需要开发人员编写JavaScript代码来处理按钮的行为。
而<input type="submit">用于创建一个表单提交按钮,用户点击它时,表单会自动提交到服务器。这个按钮会触发表单的submit事件,提交表单中的所有数据。
因此,如果需要自定义按钮行为,应该使用<input type="button">,而如果需要提交表单数据,则应该使用<input type="submit">。
<!DOCTYPE html> <html> <head> <title>Login</title> </head> <body> <h1>Login</h1> <form action="/login" method="post"> <label for="username">Username:</label> <input type="text" id="username" name="username"><br><br> <label for="password">Password:</label> <input type="password" id="password" name="password"><br><br> <input type="submit" value="Submit"> </form> </body> </html> 增加取消按钮
要在上述代码中添加一个取消按钮,可以在 `<form>` 标签的后面插入一个 `<input>` 标签。以下是修改后的代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form action="/login" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username"><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password"><br><br>
<input type="submit" value="Submit">
<input type="button" value="Cancel" onclick="window.location.href='/cancel'">
</form>
</body>
</html>
```
在上述代码中,我们添加了一个 `<input>` 标签,类型为 "button",值为 "Cancel"。同时,我们为该按钮添加了一个 `onclick` 事件,当点击该按钮时,会跳转到 "/cancel" 页面。你可以根据需要修改跳转页面的 URL。