如何使用 jQuery 将文本区域和输入框设置为只读?

jqueryweb developmentfront end technology

要将文本区域和输入框设置为只读,请使用 `attr()` 方法。

要设置只读属性,请将文本输入框和文本区域设置为只读,如下所示:

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

以下代码用于将文本区域和输入类型设置为只读:

示例

<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>

相关文章