简体   繁体   English

document.form在javascript中的含义是什么?

[英]What does document.form mean in javascript?

In JavaScript, what is the meaning of the identifiers document.cookie , document.forms and the .value field? 在JavaScript中,标识符document.cookiedocument.forms.value字段的含义是什么? I have trouble understanding the use of the below syntax example. 我无法理解下面语法示例的使用。

var x=document.forms["myForm"]["email"].value

Best wishes😀 祝福😀

document.forms["myForm"]["email"].value

that will get the value of the "email" element within the "myForm" <form> 这将获得“myForm” <form> “email”元素的value

<form id="myForm" name="myForm">
    <input id="email" name="email" value="some@email.com" />
</form>

so x will equal "some@email.com" 所以x等于“some@email.com”


document.forms will return a collection of all of the forms within a particular page. document.forms将返回特定页面中所有表单的集合。 writing document.forms["myForm"] will return the form with the name "myForm" from that collection 编写document.forms["myForm"]将返回该集合中名为“myForm”的表单

documents.forms is an object containing all of the forms for that HTML document. documents.forms是一个包含该HTML文档的所有表单的对象。 With this code, you are referencing the elements by their name attributes (not id ). 使用此代码,您将按name属性(而非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". 因此,这将提供一个字符串,其中包含name “myForm”的form name “email”的表单元素的value

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字段中的值。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM