如何从 JavaScript 中的下拉列表中删除选项?
是否有任何示例可以让我使用 JavaScript 从下拉列表中删除选项?
代码如下:
<select autocomplete="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>
这是列表 2:
<select autocomplete="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>
现在我想要发生的是,当为 1 号安全问题选择任何选项时。对于 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 autocomplete="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 autocomplete="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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过将选项设置为 null 来删除该选项。
编辑:
这是一个工作示例: http://jsfiddle.net/c5wFn/
您会做的一件事但需要解决的是,当选项更改时,您可能需要返回第一个列表项并将所有未选定的项目添加到列表中,以便放回先前选择中删除的问题。这应该不难,我只是不想处理所有细节。
也不确定你是否使用jquery,所以我使用普通的旧javascript。但是应该测试事件注册的兼容性,因为这并不适用于所有情况。
You can remove an option by setting it to null.
EDIT:
Here is a working example: http://jsfiddle.net/c5wFn/
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 compatibility as this won't work on all.