HTML - accept-charset 屬性



HTML accept-charset 屬性用於指定伺服器在提交表單時可以接受的字元編碼。

它是一個用空格分隔的伺服器接受的字元編碼列表。使用 accept-charset 可以確保在表單中輸入的字元被正確編碼並傳輸到伺服器,尤其是在處理國際字元或非 ASCII 字元的語言時。

在之前的 HTML 版本中,字元編碼也可以用逗號分隔。accept-charset 屬性僅適用於 <form> 標籤。

語法

<form accept-charset = "value"></form>

應用於

以下列出的元素允許使用 HTML accept-charset 屬性。

元素 描述
<form> HTML <form> 標籤用於指定輸入欄位。

HTML accept-charset 屬性示例

以下程式碼展示了 accept-charset 屬性可能的不同值。

指定編碼的 Accept-charset

這裡我們在 <form> 標籤內使用了 accept-charset 屬性。下面的程式碼將生成一個包含輸入欄位和點選按鈕的輸出,顯示在網頁上。

<!DOCTYPE html>
<html lang="en">

<head>
   <title>HTML 'accept-charset' attribute</title>
</head>

<body>
   <!--HTML 'accept-charset' attribute-->
   <p>HTML 'accept-charset' attribute</p>
   <form accept-charset="utf-8">
      <h1>Login</h1>
      <label for="">Username</label>
      <br>
      <input type="text">
      <br>
      <br>
      <label for="">Password</label>
      <br>
      <input type="password">
      <br>
      <br>
      <button>Login</button>
   </form>
</body>

</html>

Accept-charset 值為 'unknown'

考慮另一種情況,其中 accept-charset 屬性與表單元素一起使用,以指定用於表單提交的字元編碼(即預設值為“UNKNOWN”)。

<!DOCTYPE html>
<html lang="en">

<head>
   <title>HTML 'accept-charset' attribute</title>
   <style>
      form {
         border: 1px solid blueviolet;
         width: 300px;
         border-radius: 10px;
      }

      form h1 {
         text-align: center;
      }

      form label {
         margin: 0px 30px;
      }

      form input,
      select {
         margin: 0px 30px;
         padding: 8px;
         width: 200px;
      }

      form button {
         width: 100px;
         padding: 10px;
         margin: 0px 30px;
      }
   </style>
</head>

<body>
   <!--HTML 'accept-charset' attribute-->
   <p>HTML 'accept-charset' attribute</p>
   <form accept-charset="UNKNOWN">
      <h1>User Form</h1>
      <label for="">Name</label>
      <br>
      <input type="text">
      <br>
      <br>
      <label for="">Mobile</label>
      <br>
      <input type="number">
      <br>
      <br>
      <label for="">Select language you knows</label>
      <br>
      <br>
      <select name="language" id="">
         <option value="">Choose your option</option>
         <option value="">Hindi</option>
         <option value="">English</option>
         <option value="">Telugu</option>
      </select>
      <br>
      <br>
      <button>Submit</button>
      <br>
      <br>
   </form>
</body>

</html>

支援的瀏覽器

屬性 Chrome Edge Firefox Safari Opera
accept-charset
html_attributes_reference.htm
廣告
© . All rights reserved.