GetElementsByName包含名称之类的数组

编程入门 行业动态 更新时间:2024-10-27 07:29:02
本文介绍了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包含名称之类的数组

本文发布于:2023-10-26 08:04:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529575.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   名称   GetElementsByName

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!