我有两个问题:
这是我的代码:
$(document).ready(function() {
$('#catagory').on("change", function() {
var cattype = $(this).val();
optionswitch(cattype);
});
});
function optionswitch(myfilter) {
if ($('#optionstore').text() == "") {
$('option[class^="sub-"]').each(function() {
var optvalue = $(this).val();
var optclass = $(this).prop('class');
var opttext = $(this).text();
optionlist = $('#optionstore').text() + "@%" + optvalue + "@%" + optclass + "@%" + opttext;
$('#optionstore').text(optionlist);
});
}
$('option[class^="sub-"]').remove();
populateoption = rewriteoption(myfilter);
$('#fileType').html(populateoption);
}
function rewriteoption(myfilter) {
var options = $('#optionstore').text().split('@%');
var resultgood = false;
var myfilterclass = "sub-" + myfilter;
var optionlisting = "";
myfilterclass = (myfilter != "") ? myfilterclass : "all";
for (var i = 3; i < options.length; i = i + 3) {
if (options[i - 1] == myfilterclass || myfilterclass == "all") {
optionlisting = optionlisting + '<option value="' + options[i - 2] + '" class="sub-' + options[i - 1] + '">' + options[i] + '</option>';
resultgood = true;
}
}
if (resultgood) {
return optionlisting;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<select multiple="multiple" name="catagory" id="catagory" placeholder="Hello im from placeholder" class="search-box">
<option value="">All Food</option>
<option value="Food1">Fruit</option>
<option value="Food2">Veggies</option>
<option value="Food3">Meat</option>
<option value="Food4">Dairy</option>
<option value="Food5">Bread</option>
</select>
<select class="search-box" multiple="multiple" name="fileType" id="fileType">
<option value="Fruit1" class="sub-Food1">Apples</option>
<option value="Fruit2" class="sub-Food2">Pears</option>
<option value="Fruit3" class="sub-Food3">Bananas</option>
<option value="Fruit4" class="sub-Food4">Oranges</option>
<option value="Veg1" class="sub-Food5">Peas</option>
<option value="Veg2" class="sub-Food5">Carrots</option>
</select>
<span id="optionstore" style="display:none;"></span>
答案 0 :(得分:2)
你必须重新加注它或在追加html后更新它
$('#fileType').html(populateoption);
$("#fileType").trigger("chosen:updated");
为了多次选择而改变小提琴,你必须传递myfilter的数组