GetElementsByName包含名称之类的数组 [英] GetElementsByName with array like name

查看:114
本文介绍了GetElementsByName包含名称之类的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我命名我的控件以便在POST或GET中获取数组时,我经常使用这种表示法。

i often use this notation when i name my controls in order to get an array in POST or GET.

<input name="color[1]" type="text" />
<input name="color[2]" type="text" />
<input name="color[3]" type="text" />

所以在我的脚本中我可以做到

so in my scripts i can do

<?php $data=$_GET["color"]; 
for each ($color as $key=>$value) {
   doSomething();
} ?>

经常发生我需要在javascript中获取这些ID,但我无法得到它们,所以我经常为html中的每个元素添加一个ID,如

Often happens that i need to get those id back in javascript , but i cannot get them , so i often add an ID to each element in html like that

<input name="color[3]" id="color_3" type="text" />

因此我可以使用document.getElementsById('color_3')

so that i can use document.getElementsById('color_3')

相反,我想找到使用document.getElementsByName(color [3])...
的方法,但我无法让它真正起作用。

Instead i would like to find way to use document.getElementsByName(color[3])... but i cannot really get it to work.

任何帮助?

推荐答案

<input name="color[3]" id="color_3" type="text" />

var element = document.getElementsByName("color[3]");

alert(element[0].id);

它运作正常..你应该想到的是返回类型是一个元素数组不是单个元素

It works fine .. The thing you should have in your mind is Return type is an array of elements not a single element

这篇关于GetElementsByName包含名称之类的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆