简体   繁体   English

如何使用纯/本地javascript通过名称html标签获取元素?

[英]How to get the element by name html tag using pure/native javascript?

How to get or scan the element of different input tag name using pure/native Javascript? 如何使用纯/本地Javascript获取或扫描不同输入标签名称的元素? There's have 5 input for first, middle and last name, address and birthdate. 姓,名和姓,地址和生日有5个输入。 But I just want to get only the specific input tags and display it with alert or msgbox. 但是我只想只获取特定的输入标签,并用alert或msgbox显示它。 See below... 见下文...

Here's the html tag: 这是html标记:

<form name="test">
<tr>
        <td bgcolor="#e9e9e9"><input type="text" name="FIELD_01_01_FIRSTNAME" size="15" maxlength="60"></td>
        <td bgcolor="#e9e9e9"><input type="text" name="FIELD_01_02_MIDDLENAME" size="15" maxlength="60"></td>
        <td align="left" bgcolor="#e9e9e9"><input type="text" name="FIELD_01_03_LASTNAME" size="15" maxlength="60"></td>
      </tr>
      <tr>
        <td colspan="3" bgcolor="#e9e9e9"><input type="text" name="FIELD_01_07_ADDRESS1" size="50" value="" maxlength="200"></td>
        <td align="left" bgcolor="#e9e9e9"><input name="FIELD_01_04_BDAY" type="text" size="15" maxlength="15"></td>
      </tr>
       </form>

There's a couple ways: 有两种方法:

var list = document.querySelector("input");
var list = document.querySelectorAll("input")[0];
var list = document.getElementsByTagName("input")[0];

addendum... 附录...

querySelector is more flexible because it can take any valid css selector (much like jQuery). querySelector更加灵活,因为它可以使用任何有效的CSS选择器(与jQuery类似)。 If you need to get input elements with a particular name, this is really convenient: 如果需要获取具有特定名称的输入元素,这确实很方便:

var firstName = document.querySelector("input[name='firstName']");

var namedInputs = document.querySelectorAll("input[name]");

2nd edit... 第二次编辑...

If you don't want to type too much, you can create an alias for querySelector: 如果不想输入太多,可以为querySelector创建一个别名:

window.get = document.querySelector.bind(document);

var firstname = get("[name='FIELD_01_01_FIRSTNAME']").value;
var lastname = get("[name='FIELD_01_02_MIDDLENAME']").value;
//etc...

3rd edit... 第三编辑...

If you have your elements within a <form> , you can just use the name of the form + the name of the input for dom navigation: 如果您的元素包含在<form> ,则可以仅使用表单名称+输入名称进行dom导航:

HTML
<form name='test">
    <input type="text" name="firstName" value="James" />
</form>


//javascript
alert( test.firstName.value );

So, in this way, you could also access your inputs like so: 因此,通过这种方式,您还可以像这样访问输入:

var fname = test.FIELD_01_01_FIRSTNAME.value;
var lname = test.FIELD_01_02_MIDDLENAME.value;
//etc....

If you would like to see a demonstration of this method, try this fiddle: http://jsfiddle.net/axj6vrwb/1/ 如果您希望看到此方法的演示,请尝试以下提琴: http : //jsfiddle.net/axj6vrwb/1/

The thing to watch out for here is that you do not have a local variable named test . 这里需要注意的是,您没有名为test的局部变量。 If you want to explicitly refer to the global test , you can use window.test instead. 如果要显式引用全局test ,则可以改用window.test

Lots of answers looking at tags, but here's another approach. 查看标签的答案很多,但这是另一种方法。 If the elements are in a form, they will also be available as named properties of the form and in its elements collection, eg 如果元素为表单,则它们也可以作为表单的命名属性及其元素集合使用,例如

<form id="f0">
  <table>
    <tr>
      <td><input type="text" name="FIELD_01_01_FIRSTNAME"></td>
      <td><input type="text" name="FIELD_01_02_MIDDLENAME"></td>
      <td><input type="text" name="FIELD_01_03_LASTNAME"></td>
    </tr>
    <tr>
      <td><input type="text" name="FIELD_01_07_ADDRESS1"></td>
      <td><input name="FIELD_01_04_BDAY" type="text"></td>
    </tr>
  </table>
</form>

Then you can get all the controls in the form by first getting the form (each of the following is equivalent): 然后,您可以通过首先获取表单来获取表单中的所有控件(以下各项均等效):

var form = document.getElementById('f0');  // or
var form = document.forms[0];              // or
var form = document.forms.f0;

then get controls by name: 然后按名称获取控件:

var firstName = form.FIELD_01_01_FIRSTNAME.value;    // or
var firstName = form['FIELD_01_01_FIRSTNAME'].value;

Note that if two or more controls have the same name, the above will return an HTMLCollection of the controls. 请注意,如果两个或多个控件具有相同的名称,则上面的控件将返回控件的HTMLCollection

And using elements: 并使用元素:

var allControls = form.elements;

document.getElementsByTagName('YOUR_NAME_HERE')[0]

应该能够为您获取元素。

This question really could use so re-wording/more explanation as to what is trying to be done. 这个问题确实可以使用措词/对要做什么的更多解释。

document.querySelectorAll('input[name]');

That will select all input elements with the name attribute, which could then be iterated through. 这将选择所有具有name属性的输入元素,然后可以对其进行迭代。

or 要么

 document.getElementsByTagName('input')[0].getAttribute('name');

would give you the tag name of a particular input element. 将为您提供特定输入元素的标签名称。

But as stated, not understanding the question, I'm not sure that helps at all. 但是如前所述,由于不了解问题,我不确定这是否有帮助。

Put CSS selectors of all your inputs in a comma-separated string and run a querySelectorAll : 将所有输入的CSS选择器放入逗号分隔的字符串中,然后运行querySelectorAll

document.querySelectorAll('[name="FIELD_01_01_FIRSTNAME"], [name="FIELD_01_02_MIDDLENAME"]');

etc. 等等

var elements = document.getElementsByTagName(name);

Returns an HTMLCollection of elements with the given tag name. 返回具有给定标签名称的元素的HTMLCollection。 The complete document is searched, including the root node. 搜索完整的文档,包括根节点。 The returned HTMLCollection is live, meaning that it updates itself automatically to stay in sync with the DOM tree without having to call document.getElementsByTagName() again. 返回的HTMLCollection是实时的,这意味着它会自动更新以与DOM树保持同步,而无需再次调用document.getElementsByTagName()。

For more info please visit: document.getElementsByTagName (developer.mozilla.org) 有关更多信息,请访问: document.getElementsByTagName(developer.mozilla.org)

var elements = document.getElementsByTagName(TagName);

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

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