2

这里需要知道焦点功能的使用。谁能解释这段代码?

function Allow Age(){
  if(!form.age.value.match(/[0-9]+$/) && form.age.value !="")
  {
      form.age.value="";
      form.age.focus();
      alert("invalid format");
  }
  if(form.age.value.length > 1)
      alert("invalid entry")
}   
4

4 回答 4

1

.focus()使闪烁的行出现在文本框中,以便您可以开始输入。将来尝试先用谷歌搜索它。

于 2013-09-05T12:38:30.280 回答
1

focus()将导致光标显示在该字段中,因此当用户键入时,他们会在该字段中键入。这与用户在输入之前单击该字段时相同。

于 2013-09-05T12:39:13.000 回答
1

此代码中的第一个 If 语句是验证用户输入的 Age 是否为数字,而不是 null。第二个用于检查输入年龄的长度是否大于 1(不知道为什么会有这个,但这就是你的语句所做的)。至于您的第一个问题,Focus 用于为文本框提供闪烁的光标,即:将其设置为准备接收用户输入。

在伪代码中,它是:

function Allow Age(){
If age is not numeric and its value is not null
  Clear the textbox
  Set focus to the textbox
  Send a message saying "Invalid format"
End if

If the length of the age entered is greater than 1
  Send a message saying "Invalid Entry"
End if
}
于 2013-09-05T12:40:38.447 回答
0

这意味着获取输入焦点age中的字段。form

于 2013-09-05T12:38:40.027 回答