<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title> New Document </title> <meta name="Generator" content="EditPlus"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <script> function onReadOnly() { var o = document.getElementById("code");
{$(this).attr("readonly","true")}
else
{$(this).attr("readonly","false")}
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<script>
function onReadOnly() {
var o = document.getElementById("code");
if (o.value == "") {
if (!o.getAttribute("readOnly")) o.removeAttribute("readOnly");
} else {
o.setAttribute("readOnly", true);
document.body.focus();
}
}
window.onload = onReadOnly;
</script>
</head><body>
<input type="text" name="code" id="code" value="" onkeyup="onReadOnly()">
</body>
</html>
是不是有很多text都要符合这情况,是的话可以用下面代码:<html>
<head></head>
<script type="text/javascript">
function test()
{
//获得所有的input标签
var inputs = document.getElementsByTagName("input");
for (var i=0;i<inputs.length;i++)
{
//是否是文本框
if ("text" == inputs[i].type)
{
//是否为空
if ("" != inputs[i].value)
{
inputs[i].disabled = true;
}
else
{
inputs[i].disabled = false;
}
}
}
}
</script>
<body onload="test();">
<div id="a">
<input type="text" name="code1" id="code1" value="aaaa">
<input type="text" name="code2" id="code2" value="">
</div>
</body>
</html>
$("#code").click(function() {
if($(this).val()=='')
$(this).attr('disabled', false)
else
$(this).attr('disabled', true)
})
页面input很多,但你要处理的只是text类型的吧?代码里面不是判断了是“text”类型的才进行处理嘛。如果还要处理其它类型的,加判断就可以了
<script type="text/javascript">
onload = function(){
var obj = document.getElementById("code");
obj.disabled = (obj.value !== "");
}
</script>