根据主要选择过滤选择框选项 [英] Filter selectbox options depending on primary choice
问题描述
我正在创建一些选择框,我需要根据在主要选项中做出的选择来限制我的选项。
I am creating some selectboxes where i need to restrict my options depending on the choice made in the primary.
基本上我有选择字段1
和选择字段2
。
从数据库中解析这些选择框的数据。
Data for these selectboxes are parsed from a database.
选择字段1:
errortype_id | errortype_text
选择字段2:
errorreason_id | errortype_id | errorreason_text
我可以通过简单的方式在选择字段中对选项数据进行分类2
取决于中的选择选择字段1
?我在我的选择字段2
表中有变量/属性来过滤。
Is there a straightforward way i can customsize the option data in Select field 2
depending on the choice made in Select field 1
? I have the variable/attribute in my Select field 2
table to filter.
推荐答案
参见< a href =https://stackoverflow.com/questions/2999163/changing-options-in-select-box-based-on-different-select-options>这个问题。基本上,您需要添加更改事件并检查 $(this).val()
。从那里你只需创建选项元素并将它们作为html添加到你的选择框2。
See this question. Basically, you need to add a change event and check $(this).val()
. From there you simply create option-elements and add them as the html to your select box 2.
$('#select1').change(function() {
var options = '';
if($(this).val() == 'a') {
options = '<option value="1">1</option><option value="2">2</option>';
}
else if ($(this).val() == 'b'){
options = '<option value="3">3</option><option value="4">4</option>';
}
$('#select2').html(options);
});
编辑:我不明白你是否想要实际过滤值,移除值或添加无论你想要多少。如果您真的想要过滤掉元素,可以使用我的更改事件并将其与 .filter()。
I didn't understand if you wanted to actually filter the values, remove values, or add however many you wish. If you really want to filter out elements you can use my change event and combine that with .filter().
这篇关于根据主要选择过滤选择框选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!