gpt4 book ai didi

jquery - 筛选类别 根据另一个下拉选项选择下拉菜单

转载 作者:行者123 更新时间:2023-12-03 23:01:27 25 4
gpt4 key购买 nike

我想根据另一个下拉列表的选择来过滤选项列表。

请看下面的jquery代码;我确信我遗漏了一点点,这就是它不起作用的原因。

if($('#selectionone').is(':selected')){
$('option').filter('.edibles');

}
if($('selectiontwo').is(':selected')){
$('option').filter('.vegfats');
}

Here is the jsfiddle link

最佳答案

这是我根据所选选项添加/删除选项的方法,这适用于大多数浏览器。

我通过添加类来修改 html,首先选择选项,例如

<option class="edibles" value="Edible Oils" id="selectionone">Edible Oils</option>
<option class="vegfats" value="Vegetable Cooking Fats" id="selectiontwo">Vegetable Cooking Fats</option>

JS:

$(document).ready(function () {    
var allOptions = $('#selectprod option')
$('#selectcat').change(function () {
$('#selectprod option').remove(); //remove all options
var classN = $('#selectcat option:selected').prop('class'); //get the
var opts = allOptions.filter('.' + classN); //selected option's classname
$.each(opts, function (i, j) {
$(j).appendTo('#selectprod'); //append those options back
});
});
});

JSFiddle

关于jquery - 筛选类别 根据另一个下拉选项选择下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20477406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com