为什么此代码禁用HTML选择框不起作用?
问题描述:
function abc()
{
document.form1.1.disabled=true;
}
我在我的HTML页面中有一个选择框,其ID为1
。我正在使用上面的JavaScript,但它没有禁用选择框。为什么此代码禁用HTML选择框不起作用?
答
ID和名称标记必须以字母 ([A-ZA-Z])开始和之后可以是任意数量的字母 ,数字 ([0-9]),连字符(“ - “),下划线 (”_“),冒号(”:“)和句点 (”。“)。
- http://www.w3.org/TR/html4/types.html#type-name
解决根本问题。没有以数字开头的ID。
答
document.form1包含一个输入元素列表,selectboxes和textareas。 document.form1.1与说document.form1 [1]相同,并获得该列表中的第二个元素。尝试
document.getElementById("1").disable = true;
答
var elem = document.getElementById("1");
elem.setAttribute("disabled","disabled");