admin管理员组文章数量:1332359
I have two html select objects named with the same name (they are arrays with different indexes).
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>
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.
Thanks
I have two html select objects named with the same name (they are arrays with different indexes).
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>
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.
Thanks
Share Improve this question asked Aug 27, 2009 at 8:10 justinljustinl 10.5k22 gold badges71 silver badges90 bronze badges2 Answers
Reset to default 3Untested:
<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>
<select name='category[0]' onChange="disCat(this.value);">
<option value='0'>off</option>
<option value='1'>on</option>
</select>
<script type="text/javascript">
function disCat(val){
if(val!="0") return;
var sels=document.getElementsByTagName("select");
for(i=0;i<sels.length;i++)
{
if(sels[i].getAttribute('name')=='category[1]') sels[i].disabled=true;
}
}
</script>
本文标签: How to modify an HTML Select when the name is an array using JavascriptStack Overflow
版权声明:本文标题:How to modify an HTML Select when the name is an array using Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742316511a2451924.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论