如何使用 jQuery 使 textarea 和 input 型別只讀?


要使 textarea 和 input 型別只讀,請使用 attr() 方法。

對於只讀,將 input 型別文字和 textarea 設定為如下所示的只讀

$('input[type="text"], textarea').each(function(){
   $(this).attr('readonly','readonly');
});

以下是將只讀設定為 textarea 和 input 型別的程式碼

示例

 動態演示

<html>
<head>
<title>jQuery Example</title>
   <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
   <script>
      $(document).ready(function() {
         $('input[type="text"], textarea').each(function(){
            $(this).attr('readonly','readonly');
         });
      });

   </script>
</head>
<body>
<form>
<input type="text" value="readonly text field" /><br>
<textarea>readonly textarea</textarea>
</form>
</body>
</html>

更新於:2020 年 6 月 20 日

3K+ 次瀏覽

開始你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.