OnBlur 在 Javascript 中对函数赋值
我有这样的代码;
<script type="text/javascript">
var ID= document.getElementById('customfield_10033');
var IDNumber= document.getElementById('customfield_10033').value;
ID.onblur=function(IDNumber)
{
if ((IDNumber!="") && (IDNumber.length!=11)){
alert("Your ID number should 11 digits.");
}
但是当我输入 11 位数字的 ID 号码时,会显示警报功能。但不应该。
我认为我将 Onblur 属性分配给函数是错误的。 ID.onblur=function(IDNumber)
我该如何修复此代码?
I have a code like this;
<script type="text/javascript">
var ID= document.getElementById('customfield_10033');
var IDNumber= document.getElementById('customfield_10033').value;
ID.onblur=function(IDNumber)
{
if ((IDNumber!="") && (IDNumber.length!=11)){
alert("Your ID number should 11 digits.");
}
But when i enter ID number with 11 digits, shows me alert function. But it shouldn't.
I think i doing wrong assign Onblur property to function.. ID.onblur=function(IDNumber)
How can i fix this code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您在分配事件处理程序时定义
IDNumber
(因此它可能会保存您设置的字段默认值),但您几乎肯定希望在模糊时定义它事件发生。移至
事件处理函数内部。
(由于您已经拥有对该元素的引用,因此执行
var IDNumber = ID.value;
也会更有效)You define
IDNumber
when you assign the event handler (so it will probably be holding whatever you set the default value of the field to be), but you almost certainly want to define it when the blur event occurs.Move
Inside your event handler function.
(Since you already have a reference to the element it would be more efficient to do
var IDNumber = ID.value;
too)您应该使用类似这样的内容:
通过将
value
分配给变量,您可以放置静态数据,而不是指向该值的指针,因此您每次都必须读取“实时”值。You should have something like this instead:
By assigning the
value
into variable, you place static data, not a pointer to the value so you have to read the "live" value every time.您忘记关闭 if 语句。试试这个:
问候,
托马斯
You forgot to close the if-statement. Try this:
Regards,
Thomas