如何使用 jQuery 刪除 disabled 屬性?


如需使用 jQuery 刪除 disabled 屬性,請使用 removeAttr() 方法。您需要先使用 prop() 方法刪除該屬性。這會將底層布林值設為 false。

示例

您可以嘗試執行以下程式碼來學習如何使用 jQuery 刪除 disabled 屬性

現場演示

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){

    $('.disabledCheckboxes').prop("disabled", true);
    $('.disabledCheckboxes').removeAttr("disabled");

    $(document).ready(function(){
        $('button').on('click', function() {
          if (this.hasAttribute("disabled")) {
             alert('The disabled attribute exists')
          } else {
             alert('The disabled attribute does not exist')
          }
        })
    });
 });
</script>
</head>
<body>
<input type="checkbox" class="disabledCheckboxes" disabled>
<input type="checkbox" class="disabledCheckboxes" disabled="disabled">
   <button class='button'>Result</button>
</body>
</html>

更新時間: 2019 年 12 月 18 日

12K+ 次瀏覽

開啟您的 職業生涯

完成此課程並獲得認證

開始
廣告