我通过另一个脚本动态设置SecretAttribute价值,但目前我在做的姓名输入字段readonly
困难。
尝试设置由SecretAttribute和SecretElement,但没有骰子的getElementByID
..
<input name="SecretAttribute" id="SecretAttribute" value="Y" type="checkbox" checked/>
<html>
<head>
<title>Demo Title</title>
<script type="text/javascript">
var SecretElement = document.getElementById('SecretAttribute');\
if (document.getElementById("SecretAttribute").checked;) {
function DataCheck() {
document.getElementById('Name').readOnly = true;
}
}
</script>
</head>
<body onload="DataCheck()">
Demo6.1<br>
Name: <input type="text" id="Name" name="Name"><br>
</body>
</html>
函数调用的if语句里面。把if条件里面调用
function DataCheck() {
if(document.getElementById("SecretAttribute").checked)
document.getElementById('Name').readOnly = true;
}
var SecretElement = document.getElementById('SecretAttribute');
<input name="SecretAttribute" id="SecretAttribute" value="Y" type="checkbox" checked/>
<html>
<head>
<title>Demo Title</title>
</head>
<body onload="DataCheck()">
Demo6.1<br>
Name: <input type="text" id="Name" name="Name" value="You can't edit this"><br>
</body>
</html>
如果您使用旧版本浏览器尝试使用setAttribute,类似...
var SecretElement = document.getElementById('SecretAttribute');
if (SecretElement.checked) {
document.getElementById('Name').setAttribute("readonly","readonly");
}