gpt4 book ai didi

javascript - 使用 JavaScript 过滤下拉菜单

转载 作者:行者123 更新时间:2023-12-02 16:29:58 27 4
gpt4 key购买 nike

基于post here on StackOverflow ,我正在尝试制作一个下拉菜单,其内容取决于另一个下拉菜单的选择。

幸运的是,我找到的解决方案非常适用于我的问题,尽管我最初确实从数据库中获取了下拉菜单选项。然而不幸的是,我似乎无法让解决方案发挥作用。

两个菜单都可以从数据库中正确读取,并转换为正确的 HTML 代码。所以似乎失败的是用于过滤第二个下拉菜单中的选项的 JavaScript。我的代码如下:

<div id="selection">
<?php
// Top level part selection
echo "<select name='firstLevelParts' id='select1'>";
foreach ($category_primary as $category_item):
echo "<option value = '".$category_item['cat_primary_id']."'>".$category_item['cat_primary_name']."</option>";
endforeach;
echo "</select>";


// Second level part selection (This should be updated, based on selection in top level menu)
echo "<select name='secondLevelParts' id='select2'>";
foreach ($category_secondary as $cat_sec_item):
echo "<option value='".$cat_sec_item['cat_secondary_code']."'>".$cat_sec_item['cat_secondary_name']."</option>";
endforeach;
echo "</select>";
?>

</div>


<script>
$("#select1").change(function() {
if(typeof $(this).data('options') === "undefined")){
/*Taking an array of all options-2 and kind of embedding it on the select1*/
$(this).data('options',$('#select2 option').clone());
}
var id = $(this).val();
var options = $(this).data('options').filter('[value=' + id + ']');
$('#select2').html(options);
});
</script>

我真的希望你能帮我找出代码中的缺陷,我真的很恼火......

更新 - 根据请求,这里是生成的 HTML 代码,我无法真正给您页面 URL,因为它位于密码保护后面:

<div id="selection">
<select name='firstLevelParts' id='select1'>
<option value = '1'>Terræn</option>
<option value = '2'>Fundamentsystem</option>
<option value = '3'>Vægsystem</option>
<option value = '4'>Dæksystem</option>
<option value = '5'>Tagsystem</option>
</select>

<select name='secondLevelParts' id='select2'>
<option value='1'>Jordprofil</option>
<option value='2'>Befæstet Areal</option>
<option value='3'>Beplantning</option>
<option value='1'>Fundamentkonstruktion</option>
<option value='2'>Bærelag</option>
<option value='3'>Åbning</option>
<option value='4'>Lukning</option>
<option value='5'>Inddækning</option>
<option value='6'>Afslutning</option>
<option value='7'>Afskærmning</option>
<option value='8'>Fuge</option>
<option value='9'>Samling</option>
<option value='10'>Overflade</option>
</select>
</div>

<script>
$("#select1").change(function() {
if(typeof $(this).data('options') === "undefined")){
/*Taking an array of all options-2 and kind of embedding it on the select1*/
$(this).data('options',$('#select2 option').clone());
}
var id = $(this).val();
var options = $(this).data('options').filter('[value=' + id + ']');
$('#select2').html(options);
});
</script>

最佳答案

改编自this solution to a similar problem下面应该做你想要的:

$(function() {
$('#select').filterByValues( $('.filterElements') );
$('.filterElements').change();
});


jQuery.fn.filterByValues = function(masterSelects) {
return this.each(function() {
var select = this;
var options = [];
$(select).find('option').each(function() {
options.push({value: $(this).val(), text: $(this).text()});
});
$(select).data('options', options);

masterSelects.bind('change', function() {
var options = $(select).empty().scrollTop(0).data('options');
var vals=[];
$(masterSelects).each(function(i,e){
vals.push('^'+$.trim($(e).val())+'$' );
});
var search = vals.join('|');
var regex = new RegExp(search,'gi');

$.each(options, function(i) {
var option = options[i];
if(option.value.match(regex) !== null) {
$(select).append(
$('<option>').text(option.text).val(option.value)
);
}
});
});

});
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="selection">
<select name='firstLevelParts' class='filterElements'>
<option value = '0'>(Select category)</option> <!-- added -->
<option value = '1'>Terræn</option>
<option value = '2'>Fundamentsystem</option>
<option value = '3'>Vægsystem</option>
<option value = '4'>Dæksystem</option>
<option value = '5'>Tagsystem</option>
</select>

<select name='firstLevelPartsB' class='filterElements'>
<option value = '0'>(Select category)</option> <!-- added -->
<option value = '1'>Terræn</option>
<option value = '2'>Fundamentsystem</option>
<option value = '3'>Vægsystem</option>
<option value = '4'>Dæksystem</option>
<option value = '5'>Tagsystem</option>
</select>

<select name='secondLevelParts' id='select'>
<option value='1'>Jordprofil</option>
<option value='2'>Befæstet Areal</option>
<option value='3'>Beplantning</option>
<option value='1'>Fundamentkonstruktion</option>
<option value='2'>Bærelag</option>
<option value='3'>Åbning</option>
<option value='4'>Lukning</option>
<option value='5'>Inddækning</option>
<option value='6'>Afslutning</option>
<option value='7'>Afskærmning</option>
<option value='8'>Fuge</option>
<option value='9'>Samling</option>
<option value='10'>Overflade</option>
</select>
</div>

关于javascript - 使用 JavaScript 过滤下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28393102/

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