HTML DOM 輸入密碼停用屬性


HTML DOM 輸入密碼停用屬性用於設定或返回密碼欄位是否被停用。它使用布林值,其中 true 表示元素應被停用,false 表示否則。預設情況下,disabled 屬性設定為 false。預設情況下,停用的元素呈灰色且不可點選。

語法

以下是語法:

設定 disabled 屬性:

passwordObject.disabled = true|false;

這裡,true=密碼欄位被停用,false=密碼欄位未被停用。預設情況下為 false。

示例

讓我們來看一個輸入密碼停用屬性的示例:

線上演示

<!DOCTYPE html>
<html>
<body>
<h1>Input Password disabled Property</h1>
Password: <input type="password" id="PASS">
<p>Disable the above password field by clicking on the DISABLE button</p>
<button type="button" onclick="disablePass()">DISABLE</button>
<p id="Sample"></p>
<script>
   function disablePass() {
      document.getElementById("PASS").disabled=true;
      document.getElementById("Sample").innerHTML = "The password field is now disabled" ;
   }
</script>
</body>
</html>

輸出

這將產生以下輸出:

點選“停用”按鈕時:

點選“停用”按鈕時:

在上面的示例中:

我們首先建立了一個 id 為“PASS”的輸入密碼欄位,並且它的 disabled 屬性預設設定為 FALSE:

Password: <input type="password" id="PASS" value="abcd123">

然後,我們建立了一個名為“停用”的按鈕,當用戶點選它時,它將執行 disablePass() 方法:

<button type="button" onclick="disablePass()">DISABLE</button>

disablePass() 方法使用 getElementById() 方法獲取型別為密碼的輸入欄位,並將其 disabled 屬性設定為 true。這將停用密碼欄位並將其變灰。然後,我們使用 id 為“Sample”的段落的 innerHTML 屬性顯示一條訊息,指示密碼欄位現在已停用:

function disablePass() {
   document.getElementById("PASS").disabled=true;
   document.getElementById("Sample").innerHTML = "The password field is now disabled" ;
}

更新於: 2021年2月19日

124 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告