JQuery 下拉列表过滤器
我有第一个下拉列表 ddl1
包含以下值:
- Car
- Van
,第二个下拉列表 ddl2
包含:
- Car Honda
- Car BMW
- Van Golf
我需要一个脚本来过滤第二个 ddl 例如,当我选择 Car
时,第二个 ddl 应该只显示这两个字段:
Car Honda - 宝马汽车
我的代码:
<script type="text/javascript">
function Filter(){
var names = $('#typeCar option').clone();
$('#Type').change(function(){
var val = $(this).val();
$('#typeCar').empty();
names.filter(function(idx, el){
return val === 'ALL' || $(el).text().indexOf(val) >= 0;
}).appendTo('#typeCar');
});
}
</script>
I have first drop down list ddl1
which contains these values:
- Car
- Van
and the second drop down list ddl2
contains:
- Car Honda
- Car BMW
- Van Golf
I need a script that filters the second ddl for example when I choose Car
, the second ddl should only show these two fields:
Car Honda
- Car BMW
My code:
<script type="text/javascript">
function Filter(){
var names = $('#typeCar option').clone();
$('#Type').change(function(){
var val = $(this).val();
$('#typeCar').empty();
names.filter(function(idx, el){
return val === 'ALL' || $(el).text().indexOf(val) >= 0;
}).appendTo('#typeCar');
});
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将需要这样的内容:
第二个列表中的每个列表项值应以第一个列表中的项的相同值开头。这样就可以通过第一个值进行过滤。
编辑:
下拉菜单中的项目。
列表 1:
列表 2:
Javascript:
you will want something like this:
Each List Item value in the second list should start with the same value of the item in the first list. This is so it can be filtered by the value of the first.
EDIT:
Items in drop downs.
List 1:
List 2:
Javascript: