如何修改HTML选择当名是使用Javascript数组 [英] How to modify an HTML Select when the name is an array using Javascript
问题描述
我有相同的名称(它们与不同的索引数组)。
I have two html select objects named with the same name (they are arrays with different indexes).
我所要做的,就是如果关闭从类别[0] select元素选择,我想禁用类[1]的元素。我一直在尝试使用document.getElementsByName(),但我有没有运气搞清楚如何具体目标的类别[1]数组。见下面例如code。
What I am trying to do, is if "off" is selected from the category[0] select element, I would like to disable the category[1] element. I have been trying to use document.getElementsByName() but am having no luck figuring out how to specifically target the category[1] array. See below for example code.
<select name='category[0]'>
<option value='0'>off</option>
<option value='1'>on</option>
</select>
<select name='category[1]'></select>
我的问题是我怎么能修改HTML对象,它是一个数组的属性?我知道我能做到这一点很容易使用的ID,但我想学习如何使用数组来做到这一点。
My question is how can I modify the properties of an HTML object that is an array? I understand I could do this easily using ID's but I would like to learn how to do this using an array.
感谢
推荐答案
未经测试的:
<select name='category[0]'>
<option value='0'>off</option>
<option value='1'>on</option>
</select>
<select name='category[1]'></select>
<script>
var selects = document.getElementsByTagName('select');
for ( var i = 0, len = selects.length; i<l; ++i ) {
if ( selects[i].name == 'category[1]' ) {
// operate on selects[i];
// you can also rely on getAttribute('name')
}
}
</script>
这篇关于如何修改HTML选择当名是使用Javascript数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!