要使文本区域和输入类型为只读,请使用attr()方法。
对于只读,将输入类型text和textarea设置为只读,如下所示:
$('input[type="text"], textarea').each(function(){
$(this).attr('readonly','readonly');
});以下是将只读设置为textarea和输入类型的代码:
<html>
<head>
<title>jQuery Example</title>
<script src = "https://cdn.staticfile.org/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>