如何根据前一个选择框中的值过滤选择框? [英] How to filter select box based on value in preceding select box?
本文介绍了如何根据前一个选择框中的值过滤选择框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的Web应用程序中,我有以下两个选择框:
In my web application I have the following two select boxes:
<select id="project">
<option value="">None</option>
<option value="33">Some Project</option>
<option value="32">Best project ever</option>
<option value="31">Another project</option>
</select>
<select id="person">
<option data-project_ids="[31, 33]" value="65">John</option>
<option data-project_ids="[31]" value="66">Liz</option>
<option data-project_ids="[]" value="67">Jim</option>
</select>
使用jQuery,如何根据#project
选择框中选择的值(= project_id
)过滤#person
选项?
Using jQuery, how can I filter the #person
options based on the value (= project_id
) chosen in the #project
select box?
如果未选择任何项目,则应显示所有人.
If no project is chosen, then all persons should be displayed.
在页面加载时也可能会预先选择一个项目.
It might also happen that a project is pre-selected on page load.
推荐答案
选项1
使用 indexOf :
(感谢T.J. Crowder的有用评论)
(Thanks to helpful comment by T.J.Crowder)选项2
Option 2