HTML enctype 属性

htmlweb developmentfront end technology更新于 2024/2/27 16:44:17

HTML enctype 属性指定表单数据的编码格式。使用 post 方法时,可以指定 enctype 属性。

此处,enctype 属性可以具有以下值 −

值
描述
application/x-www-form-urlencoded
这是默认值。所有字符在发送前都会进行编码(空格转换为"+"符号,特殊字符转换为 ASCII 十六进制值)
multipart/form-data
不对字符进行编码。如果用户使用的表单包含文件上传控件,则此值是必需的
text/plain
使用此 enctype 值,空格将转换为"+"符号,但不编码任何特殊字符

让我们看一个 HTML enctype 属性的示例:

示例

<!DOCTYPE html>
<html>
<head>
<title>HTML enctype attribute</title>
<style>
   form {
      width:70%;
      margin: 0 auto;
      text-align: center;
   }
   * {
      padding: 2px;
      margin:5px;
   }
   input[type="button"] {
      border-radius: 10px;
   }
</style>
</head>
<body>
<form enctype="multipart/form-data" action="" method="post">
<fieldset>
<legend>HTML-enctype-attribute</legend>
<label for="EmailSelect">Email Id:
<input type="email" id="EmailSelect">
<input type="button" onclick="getUserEmail('david')" value="David">
<input type="button" onclick="getUserEmail('shasha')" value="Shasha"><br>
<input type="button" onclick="login()" value="Login">
</label>
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
   var divDisplay = document.getElementById("divDisplay");
   var inputEmail = document.getElementById("EmailSelect");
   function getUserEmail(userName) {
      if(userName === 'david')
         inputEmail.value = 'davidMiller@MNC.com';
      else
         inputEmail.value = 'shashaGreen@MNC.com';
   }
   function login() {
      if(inputEmail.value !== '')
         divDisplay.textContent = 'Successful Login. Hello '+inputEmail.value.split("@")[0];
      else
         divDisplay.textContent = 'Enter Email Id';
   }
</script>
</body>
</html>

输出

这将产生以下输出 −

1) 点击"登录"按钮,邮箱地址为空 −

2) 点击"登录"按钮,邮箱地址为空 −


相关文章


有用资源