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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 13:44:09  来源:igfitidea点击:

What does document.form mean in javascript?

javascriptforms

提问by dramasea

In JavaScript, what is the meaning of the identifiers document.cookie, document.formsand the .valuefield? I have trouble understanding the use of the below syntax example.

在JavaScript中,什么是标识的含义document.cookiedocument.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 valueof 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 xwill equal "[email protected]"

所以x将等于“[email protected]



document.formswill 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.formsis an object containing all of the forms for that HTML document. With this code, you are referencing the elements by their nameattributes (not id). So this would provide a string containing the valuefor the form element with the name"email" within the formwith the name"myForm".

documents.forms是一个包含该 HTML 文档的所有表单的对象。使用此代码,您将通过元素的name属性(而不是id)来引用元素。因此,这将提供一个字符串,其中包含带有“myForm”valuename“email”的表单元素。formname

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_emailvariable would then contain the value entered into the inputfield.

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>