Javascript javascript中的document.form是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4715050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does document.form mean in javascript?
提问by dramasea
In JavaScript, what is the meaning of the identifiers document.cookie
, document.forms
and the .value
field? I have trouble understanding the use of the below syntax example.
在JavaScript中,什么是标识的含义document.cookie
,document.forms
和.value
现场?我无法理解以下语法示例的使用。
var x=document.forms["myForm"]["email"].value
Best wishes
最好的祝愿
回答by hunter
document.forms["myForm"]["email"].value
document.forms["myForm"]["email"].value
that will get the value
of the "email" element within the "myForm" <form>
这将获得value
“myForm”中“email”元素的<form>
<form id="myForm" name="myForm">
<input id="email" name="email" value="[email protected]" />
</form>
so x
will equal "[email protected]"
所以x
将等于“[email protected]”
document.forms
will return a collection of all of the forms within a particular page. writing document.forms["myForm"]
will return the form with the name "myForm" from that collection
document.forms
将返回特定页面内所有表单的集合。写作document.forms["myForm"]
将从该集合中返回名称为“myForm”的表单
回答by Kyle
documents.forms
is an object containing all of the forms for that HTML document. With this code, you are referencing the elements by their name
attributes (not id
). So this would provide a string containing the value
for the form element with the name
"email" within the form
with the name
"myForm".
documents.forms
是一个包含该 HTML 文档的所有表单的对象。使用此代码,您将通过元素的name
属性(而不是id
)来引用元素。因此,这将提供一个字符串,其中包含带有“myForm”value
的name
“email”的表单元素。form
name
Example:
例子:
<form name="contact-form">
Email: <input type="text" name="email" />
</form>
Executing the following JavaScript code at anytime when a value for the email field is desired would provide the value.
当需要电子邮件字段的值时,随时执行以下 JavaScript 代码将提供该值。
var contact_email = document.forms["contact-form"]["email"].value;
The contact_email
variable would then contain the value entered into the input
field.
contact_email
然后该变量将包含输入到该input
字段中的值。
回答by indranatha madugalle
this code shows how you can use document.forms in an example with validation.
``
``
function validation(inputs){
if (inputs==""|| inputs=="null"){
alert("Enter Valid Number");
return false;
}
if (isNaN(inputs)){
alert("Enter Valid Number");
return false;
}
return true;
}
function triNum(num){
var triangle=0;
for(i=1 ;i <= num; i++){
triangle += i;
}
return triangle;
}
function squareNum(num){
var square = num * num;
return square;
}
function findNums(){
//var num = document.getElementById('number1').value;
var num= document.forms["MagicNum"]["FirstNum"].value;
if (validation(num)){
document.forms["MagicNum"]["tri"].value=triNum(num);
document.forms["MagicNum"]["square"].value=squareNum(num);
}
}
</script>