如何用Javascript代码控制input标签的readonly或disabled属性
如何用Javascript代码控制input标签的readonly或disabled属性,我想用一个按钮,控制一个文本框里的值是否可以被修改,但是不知道如何在js里控制标...
如何用Javascript代码控制input标签的readonly或disabled属性,我想用一个按钮,控制一个文本框里的值是否可以被修改,但是不知道如何在js里控制标签的属性。麻烦高手指点一下~
展开
3个回答
2013-08-09
展开全部
举readonly个简单的例子,代码如下:========<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head><body>
<form action="#" method="post">
<input type="text" id="txt"/>
<input type="button" id="btn" value="切换成readonly状态" onclick="test()" />
</form>
<script type="text/javascript">
function test(){
var txt=document.getElementById("txt");
var btn=document.getElementById("btn");
if(txt.readOnly)
{
txt.readOnly=false;
btn.value="切换成readonly状态";
}
else
{
txt.readOnly=true;
btn.value="切换成可以输入状态";
}
}
</script>
</body>
</html>
============disabled类似,代码如下:==============<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head><body>
<form action="#" method="post">
<input type="text" id="txt"/>
<input type="button" id="btn" value="切换成disabled状态" onclick="test()" />
</form>
<script type="text/javascript">
function test(){
var txt=document.getElementById("txt");
var btn=document.getElementById("btn");
if(txt.disabled)
{
txt.disabled=false;
btn.value="切换成disabled状态";
}
else
{
txt.disabled=true;
btn.value="切换成可以输入状态";
}
}
</script>
</body>
</html>
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head><body>
<form action="#" method="post">
<input type="text" id="txt"/>
<input type="button" id="btn" value="切换成readonly状态" onclick="test()" />
</form>
<script type="text/javascript">
function test(){
var txt=document.getElementById("txt");
var btn=document.getElementById("btn");
if(txt.readOnly)
{
txt.readOnly=false;
btn.value="切换成readonly状态";
}
else
{
txt.readOnly=true;
btn.value="切换成可以输入状态";
}
}
</script>
</body>
</html>
============disabled类似,代码如下:==============<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head><body>
<form action="#" method="post">
<input type="text" id="txt"/>
<input type="button" id="btn" value="切换成disabled状态" onclick="test()" />
</form>
<script type="text/javascript">
function test(){
var txt=document.getElementById("txt");
var btn=document.getElementById("btn");
if(txt.disabled)
{
txt.disabled=false;
btn.value="切换成disabled状态";
}
else
{
txt.disabled=true;
btn.value="切换成可以输入状态";
}
}
</script>
</body>
</html>
2013-08-09
展开全部
<input type="text" id="Cs" value="123" />
<script type="text/javascript">
var Js_Cs= document.getElementById("Cs");
Js_Cs.readOnly=true;
</script>PS:此时JavaScript代码要写到<input>标签后面
<script type="text/javascript">
var Js_Cs= document.getElementById("Cs");
Js_Cs.readOnly=true;
</script>PS:此时JavaScript代码要写到<input>标签后面
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-09
展开全部
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head><body>
<input type="text" name="mytext" id="mytext" />
<script type="text/javascript">
//readOnly
document.getElementById("mytext").readOnly = true;
//disabled
document.getElementById("mytext").disabled = true;
</script>
</body>
</html>
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head><body>
<input type="text" name="mytext" id="mytext" />
<script type="text/javascript">
//readOnly
document.getElementById("mytext").readOnly = true;
//disabled
document.getElementById("mytext").disabled = true;
</script>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询