通过 JavaScript 获取表单名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16731938/
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-10-27 05:47:06  来源:igfitidea点击:

Get form name via JavaScript

javascript

提问by Nave Tseva

I have simple question but I can't fined good solution on the web.

我有一个简单的问题,但我无法在网上找到好的解决方案。

I have this HTML code:

我有这个 HTML 代码:

 <form name="Register" action="Register.aspx" method="post" runat="server" style="margin-top: 15px;" onsubmit="return validateProfile(this);" >

And this JavaScript code

还有这段 JavaScript 代码

function validateProfile(F) {
var G = F.name; 
}

I want somehow to get the form name, but this code just does not working.

我想以某种方式获取表单名称,但此代码不起作用。

wish for help, thanks!

希望得到帮助,谢谢!

回答by Benjamin Gruenbaum

There you go

你去吧

function validateProfile(F) {
    alert(F.name); 
    return false;
}

Fis already the form, no need to use .Form.

F已经是表格了,不用再用了.Form

Since a form is an element, you can access its name using .name.

由于表单是一个元素,您可以使用.name.

This is defined in the DOM specification here:

这是在此处DOM 规范中定义的:

nameof type DOMString

Names the form.

DOMString 类型的名称

命名表单。

Demo

演示

Notice how my JSFiddle contains window.validateProfile = validateProfilebecause I run it after the DOM is ready, if your function is not directly in a script block, chances are you need to do this too.

请注意我的 JSFiddle 包含的内容,window.validateProfile = validateProfile因为我在 DOM 准备好后运行它,如果您的函数不是直接在脚本块中,您可能也需要这样做。

回答by RobG

You likely have a control in the form with a name of name. Form controls are made available as properties of the form, using their name or ID as the property name. So in:

您可能有一个名为name的表单控件。表单控件可用作表单的属性,使用它们的名称或 ID 作为属性名称。所以在:

<form name="foo" ...>
  <input name="name" ...>

then:

然后:

document.forms['foo'].name

returns a reference to the input element, not the value of the form's nameproperty (which reflects the value of the HTML name attribute).

返回对 input 元素的引用,而不是表单的name属性的值(它反映了 HTML name 属性的值)。

The solution is to not use attribute values for form controls that are the same as standard form element attribute or DOM property names (e.g. do not name form controls "submit" or "reset" as they will overwrite the form's submitand resetmethods).

解决方案是不要使用与标准表单元素属性或 DOM 属性名称相同的表单控件的属性值(例如,不要将表单控件命名为“提交”或“重置”,因为它们会覆盖表单的提交重置方法)。

回答by Dhaval Marthak

I wonder why your code is not working

我想知道为什么你的代码不起作用

Its working here

它在这里工作

function validateProfile(F) {
var G = F.name; 
    alert(G);
    return false;
}

Demo

演示

回答by PSR

  var  theForms = document.getElementsByTagName("form");  
    for(i=0;i<theForms.length;i++)  
    alert(theForms[i].name);  

or if you have only one form then use theForms [0].namedirectly

或者如果您只有一种形式,则theForms [0].name直接使用

回答by Jeff Lee

If you use Jquery. You can get the attr by this :

如果您使用 Jquery。您可以通过以下方式获得 attr:

$("form").attr("name"); // Register