如何使用 jQuery 在單擊按鈕時啟用“停用”屬性?


在 jQuery 中將停用屬性設定為 true −

<input type="text" disabled=true id="showTxtBoxHide" value="My Name is David..">

示例

以下是程式碼 −

 即時演示

<!DOCTYPE html>
<html lang="en">
 
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
 
<body>
  <input type="text" disabled=true id="showTxtBoxHide" value="My Name is David..">
  <br>
  <br>
  <input type="button" id="change" value="ClickTheButtonToEnableTextBox">
</body>
<script>
  $("#change").click(function (buttonEvent) {
    buttonEvent.preventDefault();
    $('#showTxtBoxHide').prop("disabled", false);
  });
</script>
 
</html>

要執行上述程式,請將檔名儲存為 anyName.html(index.html)。在 VS Code 編輯器中右鍵單擊檔案,然後選擇選項“使用 Live Server 開啟” −

輸出

輸出如下 −

文字框屬性已停用。要啟用,你需要單擊按鈕 −

更新於:03-10-2020

2K+ 瀏覽量

啟動你的 職業生涯

透過完成課程獲得認證

開始
廣告