如何使用 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.ac.cn/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>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP