2 回答

TA貢獻1744條經(jīng)驗 獲得超4個贊
我不知道這段代碼的上下文,所以我不知道這種“嵌套”選項和選擇模式是否是最佳解決方案。但這是我的 javascript 版本,它似乎實現(xiàn)了問題中的要求。我會評論相關(guān)的變化,這樣我的思維過程就很清楚了。我還為隱藏類添加了 css,因為它不在 jsfiddle 中。
$(document).ready(function () {
$("select").on('change', function () {
var selClass = $(this).attr("class");
var selName = $(this).attr("name");
var selVal = $(this).children("option:selected").val();
//Call the update function with the data of the changed Select
updateRecursivly(selClass, selName, selVal);
//Recursive function to update all selects, this allows for multiple "child" selects per select and an in theory infinite "tree" of selects
function updateRecursivly(selClass, selName, selVal) {
//Search "children" of the parent select
var children = $("select." + selClass + "[data-parent='" + selName + "']");
if (children.length) {
children.each(function () {
//Hide all options in the "child" select
$(this).children("option[data-parent]").hide();
//if selVal is an empty string, the default option is selected and we should just hide the "child" select
if (selVal !== "") {
//Get all options that contain (*=) selVal in "data-parent"
var options = $(this).children("option[data-parent*='" + selVal + "']");
//If there are possible options show the select and the possible options. Else hide select
if (options.length) {
$(this).removeClass('hide');
options.show();
} else {
$(this).addClass('hide');
}
} else {
$(this).addClass('hide');
}
//If the select is updated, the options should be reset. Any selected is reset and the first is selected
//From here https://stackoverflow.com/a/16598989/14040328 this is apparently safer against reset events than other solutions
$(this).children("option:selected").prop("selected", false);
$(this).children("option:first").prop("selected", "selected");
//Get the name of the select
var childName = $(this).attr("name");
//Update the Child select
updateRecursivly(selClass, childName, "");
});
}
}
});
});
.hide {
display: none;
}
我不確定我是否過度評論了,如果有任何不清楚的地方,請隨時發(fā)表評論。

TA貢獻1841條經(jīng)驗 獲得超3個贊
您可以使用attributeContainsWord (~)將多個值與一個詞匹配。因此,在下面的代碼中,我使用$(this).children("option[data-parent~='" + selVal + "']").show();
顯示與匹配的選項selVal
。另外,我使用 prop('selectedIndex', 0);
將選擇重置為默認(rèn)值。
演示代碼:
$(document).ready(function() {
$(".hide").children("option").hide();
$("select").on('change', function() {
//checking if dropdown is categories
if ($(this).attr("name") == "categories") {
//reset other to default
$('select[name=desserts]').prop('selectedIndex', 0);
$('select[name=flavors]').prop('selectedIndex', 0);
}
var selClass = $(this).attr("class");
var selName = $(this).attr("name");
var selVal = $(this).children("option:selected").val();
//check if name != desserts
if ($(this).attr("name") != "desserts") {
//hide option of flavour
$("select[name=flavors]").children("option").hide();
//loop and show desire values
$("select." + selClass + "[data-parent='" + selName + "']").each(function() {
$(this).children("option[data-parent='" + selVal + "']").show();
$(this).children("option[data-parent!='" + selVal + "']").hide();
});
} else {
//hide options
$("select[name=flavors]").children("option").hide();
//loop through flavours
$("select[name=flavors]").each(function() {
//use " ~ " to see if the selval matches with values
$(this).children("option[data-parent~='" + selVal + "']").show();
});
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="group-1" name="categories">
<option value="" selected="selected">Please select...</option>
<option value="cookie">Cookies</option>
<option value="cake">Cakes</option>
<option value="icecream">Ice Cream</option>
</select>
<select class="group-1 hide" name="desserts" data-parent="categories">
<option value="" selected="selected">Desserts...</option>
<option value="1-dozen" data-parent="cookie">1 dozen</option>
<option value="2-dozen" data-parent="cookie">2 dozen</option>
<option value="sheet" data-parent="cake">Sheet</option>
<option value="round" data-parent="cake">Round</option>
<option value="pint" data-parent="icecream">Pint</option>
</select>
<select class="group-1 hide" name="flavors" data-parent="desserts">
<option value="" selected="selected">Flavors...</option>
<option value="choc-chip" data-parent="1-dozen 2-dozen">Chocolate Chip</option>
<option value="oatmeal" data-parent="1-dozen 2-dozen">Oatmeal</option>
<option value="yellow" data-parent="sheet round">Yellow</option>
<option value="red-velvet" data-parent="sheet">Red Velvet</option>
</select>
添加回答
舉報