有人可以解释一下这段代码吗? [英] Can someone please explain this piece of code?
问题描述
function validateForm(){
var x = document.forms [myForm] [fname]。value;
if(x == ){
alert(姓名必须填写);
返回false;
}
}
我尝试了什么:
请解释第二行为什么我们需要使用return false?
function validateForm() {
var x = document.forms["myForm"]["fname"].value;
if (x == "") {
alert("Name must be filled out");
return false;
}
}
What I have tried:
Explain the second line please and why we need to use return false?
推荐答案
document.forms
将返回所有表单的集合在一个页面内。
所以document.forms [myForm]
将返回该集合中名为myForm的表单。
和document.forms [myForm] [myElement]
将返回该表单的元素 - 例如控件。
所以document.forms [myForm] [myElement]。value
将返回该控件中的当前数据。
即在您的情况下
document.forms
will return a collection of all of the forms within a page.
Sodocument.forms["myForm"]
will return the form with the name "myForm" from that collection.
Anddocument.forms["myForm"]["myElement"]
will return an element of that form - a control for example.
Sodocument.forms["myForm"]["myElement"].value
will return the current data in that control.
I.e. in your case
var x = document.forms["myForm"]["fname"].value;
获取用户在控件 fname
中键入的内容并存储在名为的变量中 x
这是一个验证功能:它的目的是告诉外界用户输入的是什么可以接受的。如果不是,则返回 false
来表示。
Gets what the user typed in the control fname
and stores in in a variable called x
This is a validation function: it's purpose is to tell the outside world if what the user typed was acceptable. If it isn't, it returns false
to indicate that.
这篇关于有人可以解释一下这段代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!