控制input框只读可以防止用户对数据的更改,在某些情况下还是比较实用的,下面使用js来完成这个只读实现
<html>
<head>
<title> new document </title>
<meta name="generator" content="editplus">
<meta name="author"
content="">
<meta name="keywords" content="">
<meta
name="description" content="">
</head>
<script
language="javascript">
function isreadonly(){
var obj =
document.getelementbyid("username");
obj.setattribute("readonly",true);
obj.style.backgroundcolor="#d2d2d2";
}
function readwrite(){
var
obj = document.getelementbyid("username");
obj.setattribute("readonly",false);
obj.style.backgroundcolor="#ffffff";
}
</script>
<body>
<form name="addform"
id="addform" method="post" action="" >
<input type="text"
id="username" name="username">
<input type="button" name="只读"
value="read" onclick="isreadonly();">
<input type="button" name="可写"
value="write" onclick="readwrite();">
</form>
</body>
</html>
<!--
点击“read”按钮,input框不能书写,且变灰;点击“write”按钮,input框恢复。
-->
以上就是js设置input文本框只读的详细内容。
相关文章:
通过js设置所有form对象为只读
处理表单使input等文本框为只读不可编辑的方法
使用js动态控制input框的只读属性