我的问题是可以在被配置为只接受一定数目的字符?我可以在以下JavaScript中执行此操作,但是我正在设计此页面的人员不需要状态字段。
我目前的做事方式是我有一个,它是一个表单的一部分,它使用一个漂亮的JavaScript函数来填充状态框中的消息。
我也读过并发现警报和声音真的不是提醒人们的方式,所以下面的代码会在出现错误时更改背景颜色(并在适当时进行恢复)。
这里是代码:
// Checks Form element TransDesc for overruns past 255 characters.
function warnOverDescLen()
{
var misc_text =
document.forms["InvGenPayTickets"]["TransDesc"].value;
var alert_text =
"You cannot enter more than 255 characters. Please remove some information.";
var rc = true;
if(255 < misc_text.length)
{
document.forms["InvGenPayTickets"]["trans_status"].value
= alert_text;
misc_text = misc_text.substring(0, 253);
document.forms["InvGenPayTickets"]["TransDesc"].value
= misc_text;
document.forms["InvGenPayTickets"]["trans_status"].style.backgroundColor
= "pink";
}
else
{
document.forms["InvGenPayTickets"]["trans_status"].value
= "";
document.forms["InvGenPayTickets"]["trans_status"].style.backgroundColor
= "lightgoldenrodyellow";
}
return rc;
}
οnkeypress="return warnOverDescLen();" >
Status
size="65" />
+0
参见例如http://stackoverflow.com/questions/451491/what-is-the-best-way-to-emulate-an-html-input-maxlength-attribute-on-an-html-t –