HTML DOM Input Search readOnly 属性

htmlweb developmentfront end technology更新于 2025/9/26 10:52:17

HTML DOM Input Search readOnly 属性用于设置或返回输入搜索字段是否为只读。readOnly 属性使元素不可编辑,但仍可通过 Tab 键或点击获得焦点。如果只读元素中包含默认值,则在提交时会将其发送到服务器。

语法

以下是语法 −

设置 readOnly 属性 −

searchObject.readOnly = true|false

此处,true 表示搜索字段为只读,false 表示非只读。readOnly 属性默认设置为 false。

示例

让我们来看一个 Search readOnly 属性的示例 −

<!DOCTYPE html>
<html>
<body>
<h1>Search readOnly property</h1>
<form>
FRUITS: <input type="search" id="SEARCH1" name="fruits">
</form>
<p>Change the readOnly property of the above field by clicking the below button</p>
<button onclick="changeRead()">CHANGE</button>
<script>
   function changeRead() {
      document.getElementById("PASS1").readOnly = true;
   }
</script>
</body>
</html>

输出

这将产生以下输出 −

点击"更改"按钮 −


相关文章


有用资源