admin管理员组文章数量:1287809
Are there any examples out there that will allow me to remove options from drop down list using JavaScript?
Here is the code:
<select autoplete="off" id="sSec1" tabindex="0" name="sSec1" class="isSelected styled security selectOriginal">
<option id="" value="">Select</option>
<option id="o1" value="What is the name of your first pet?">What is the name of your first pet?</option>
<option id="o2" value="What is your favorite food?">What is your favorite food?</option>
<option id="o3" value="What is your favorite holiday?">What is your favorite holiday?</option>
<option id="o4" value="What is your favorite movie?">What is your favorite movie?</option>
<option id="o5" value="What is your favorite sport?">What is your favorite sport?</option>
<option id="o6" value="In what city were you born?">In what city were you born?</option>
<option id="o7" value="What is your mother's maiden name?">What is your mother's maiden name?</option>
</select>
Here is list 2:
<select autoplete="off" id="sSec2" tabindex="0" name="sSec2" class="isSelected security styled">
<option id="" value="">Select Second Question</option>
<option id="o1s2" value="What is the name of your first pet?">What is the name of your first pet?</option>
<option id="o2s2" value="What is your favorite food?">What is your favorite food?</option>
<option id="o3s2" value="What is your favorite holiday?">What is your favorite holiday?</option>
<option id="o4s2" value="What is your favorite movie?">What is your favorite movie?</option>
<option id="o5s2" value="What is your favorite sport?">What is your favorite sport?</option>
<option id="o6s2" value="In what city were you born?">In what city were you born?</option>
<option id="o7s2" value="What is your mother's maiden name?">What is your mother's maiden name?</option>
</select>
Now what I want to happen is when any option is selected for security question number 1. That option should not be there for security question number 2.
Are there any examples out there that will allow me to remove options from drop down list using JavaScript?
Here is the code:
<select autoplete="off" id="sSec1" tabindex="0" name="sSec1" class="isSelected styled security selectOriginal">
<option id="" value="">Select</option>
<option id="o1" value="What is the name of your first pet?">What is the name of your first pet?</option>
<option id="o2" value="What is your favorite food?">What is your favorite food?</option>
<option id="o3" value="What is your favorite holiday?">What is your favorite holiday?</option>
<option id="o4" value="What is your favorite movie?">What is your favorite movie?</option>
<option id="o5" value="What is your favorite sport?">What is your favorite sport?</option>
<option id="o6" value="In what city were you born?">In what city were you born?</option>
<option id="o7" value="What is your mother's maiden name?">What is your mother's maiden name?</option>
</select>
Here is list 2:
<select autoplete="off" id="sSec2" tabindex="0" name="sSec2" class="isSelected security styled">
<option id="" value="">Select Second Question</option>
<option id="o1s2" value="What is the name of your first pet?">What is the name of your first pet?</option>
<option id="o2s2" value="What is your favorite food?">What is your favorite food?</option>
<option id="o3s2" value="What is your favorite holiday?">What is your favorite holiday?</option>
<option id="o4s2" value="What is your favorite movie?">What is your favorite movie?</option>
<option id="o5s2" value="What is your favorite sport?">What is your favorite sport?</option>
<option id="o6s2" value="In what city were you born?">In what city were you born?</option>
<option id="o7s2" value="What is your mother's maiden name?">What is your mother's maiden name?</option>
</select>
Now what I want to happen is when any option is selected for security question number 1. That option should not be there for security question number 2.
Share Improve this question edited Dec 31, 2010 at 18:41 IAdapter 64.8k73 gold badges186 silver badges243 bronze badges asked Dec 31, 2010 at 18:09 AmenAmen 7134 gold badges15 silver badges28 bronze badges1 Answer
Reset to default 7You can remove an option by setting it to null.
document.getElementById("sSec1").options[0] = null;
EDIT:
Here is a working example: http://jsfiddle/c5wFn/
document.getElementById("sSec1").addEventListener("change",
function(select)
{
if (this.value != "")
{
var select2 = document.getElementById("sSec2");
for (var i = 0; i < select2.options.length; i++)
{
if (this.value == select2.options[i].value)
{
select2.options[i] = null;
break;
}
}
}
}, false);
The one thing that you will need to work in though is that when an option is changed you need to probably go back through the first list items and add all the non selected items to the list so you put back the question that was removed with the previous selection. It shouldn't be hard I just didn't want to work up all the details.
Also not sure if you are using jquery or not so I worked up with plain old javascript. But the event registration should be tested for patibility as this won't work on all.
本文标签: selectHow do I remove options from a dropdownlist in javascriptStack Overflow
版权声明:本文标题:select - How do I remove options from a dropdownlist in javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741329501a2372686.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论