【发布时间】:2019-10-14 16:26:55
【问题描述】:
根据“地区”(html <select id="regioes">) 的组合选择,“程序”(html <select id="programas">) 食物或带有 Json (list-region-programs.json) 列表的组合框下方的 JS 代码。
但是我如何使用 Json 列表 combox-municipios.json 在“区域”中提供或组合框“基于组合”的城市 (html <select id="municipios">)?
选择 1 个程序 -> 提供区域 -> 选择 1 个区域 -> 提供“县”(3 个嵌套组合)。
HTML
<div class="form-group">
<!-- dropdown Regiões -->
<label for="regioes">Região</label>
<select id="regioes">
<option value=""></option>
</select>
</div>
<div class="form-group">
<!-- dropdown Programas -->
<label for="programas">Programa </label>
<select id="programas">
</select>
</div>
JS JSON
$(document).ready(function(){
$.getJSON('SIGPLAM2/javaScript/lista-regiao-programas.json', function(data){
var items = [];
var options = '<option value="">escolha uma região</option>';
$.each(data, function(key, val){
options += '<option value="' + val.nome + '">' + val.nome + '</option>';
});
$("#regioes").html(options);
$("#regioes").change(function(){
var options_programas = '';
var str = "";
$("#regioes option:selected").each(function(){
str += $(this).text();
});
$.each(data, function(key, val){
if(val.nome == str){
$.each(val.cidades, function(key_city, val_city){
options_programas += '<option value="' + val_city + '">' + val_city + '</option>';
});
}
});
$("#programas").html(options_programas);
}).change();
});
});
【问题讨论】: