【发布时间】:2010-10-12 15:26:15
【问题描述】:
我正在尝试使用 JavaScript 动态设置输入字段的最大长度。显然这是 IE 中的一个问题,我找到了部分解决方案。
$("input#title").get(0).setAttribute("max_length", 25);
$("input#title").get(0).setAttribute(
"onkeypress",
"return limitMe(event, this)");
function limitMe(evt, txt) {
if (evt.which && evt.which == 8) return true;
else return (txt.value.length < txt.getAttribute("max_length");
}
它可以在 Firefox 中运行,但由于某种原因不能在 IE 中运行。但是,它适用于这样设置的输入字段:
<input type="text" max_length="25" onkeypress="return limitMe(event, this);"/>
但由于输入字段是动态创建的,我不能这样做...有什么想法吗?
【问题讨论】:
标签: javascript html forms