根据主要选择过滤选择框选项

我正在创建一些选择框,我需要根据主要选项中的选择来限制我的选项。

基本上我有Select field 1Select field 2

这些选择框的数据是从数据库中解析出来的。

选择字段1:

 errortype_id | errortype_text 

选择字段2:

 errorreason_id | errortype_id | errorreason_text 

是否有一种直接的方法可以根据在Select field 1中的Select field 1Select field 2的选项数据? 我在Select field 2表中有变量/属性来过滤。

在此处输入图像描述

看到这个问题 。 基本上,您需要添加更改事件并检查$(this).val() 。 从那里你只需创建选项元素并将它们作为html添加到你的选择框2。

 $('#select1').change(function() { var options = ''; if($(this).val() == 'a') { options = ''; } else if ($(this).val() == 'b'){ options = ''; } $('#select2').html(options); }); 

编辑:我不明白你是否想要实际过滤值,删除值,或添加你想要的多少。 如果您真的想要过滤掉元素,可以使用我的更改事件并将其与.filter()结合使用。