【发布时间】:2014-04-27 08:13:56
【问题描述】:
我有一个文本模式设置为多行的文本框。这样做会将文本框呈现为文本区域。大意是这样的:
<asp:TextBox ID="txtFinBillingTerms" maxlength="500" runat="server" ToolTip="(e.g. 90/10, 30/30/30/10, etc.)" TextMode="MultiLine" Columns="5" Rows="5" Width="300px"></asp:TextBox>
我遇到的问题是,当我运行我的项目并检查呈现的 html 的 textarea 时,它没有显示 maxlength 属性,就好像它消失了:
渲染的html:
<textarea name="ctl00$MainContent$txtFinBillingTerms" rows="5" cols="5" id="MainContent_txtFinBillingTerms" title="(e.g. 90/10, 30/30/30/10, etc.)" style="width:300px;"></textarea>
这给我带来了问题,因为我试图放入一些 javascript/jquery 来限制我的 textarea 的输入,即:
$('textarea').keypress(function (e) {
var maxLength = $(this).attr('maxlength');
alert(maxLength);
if (e.which < 0x20) {
// e.which < 0x20, then it's not a printable character
// e.which === 0 - Not a character
return; // Do nothing
}
if (this.value.length == max) {
e.preventDefault();
} else if (this.value.length > max) {
// Maximum exceeded
this.value = this.value.substring(0, max);
}
});
但 maxLength 始终未定义....
【问题讨论】:
-
您正在使用 var maxLength 但稍后在代码中您只使用 max。
-
e.which怎么可能小于0x20?你想在这里做什么,x不是乘数,*是,但是乘以零总是返回零,所以有点不清楚意图是什么? -
杰夫的错误我必须解决这个问题。 adeneo 我不想在这里乘以 0x20 是十六进制。
标签: javascript jquery html asp.net