按给定字典填充 select 字段
Populating select field by given dictionary
我找到了一个示例,它通过给定的 html 字符串填充 select 字段:
<script>
$(document).ready(function() {
$("#shape").change(function() {
var val = $(this).val();
$("#size").html(options[val]);
});
var options = [
"<option value='test'>item1: test 1</option><option value='test2'>item1: test 2</option>",
"<option value='test'>item2: test 1</option><option value='test2'>item2: test 2</option>",
"<option value='test'>item3: test 1</option><option value='test2'>item3: test 2</option>"
];
});
</script>
哪个工作正常,但我必须如何更改脚本才能对给定的字典执行相同的操作?
options = {
'ipe': ['80', '100'],
'hea': ['90', '110'],
}
这里用一张图来说明:
这是我的 html 片段:
<div class="panel panel-primary">
<div class="panel-body">
<table>
<tr>
<td>{{ form.shape.label }}</td>
<td>{{ form.shape(class="form-control field", id="shape") }}</td>
<td>{{ widgets.errors(form.shape.errors) }}</td>
</tr>
<tr>
<td>{{ form.size.label }}</td>
<td>{{ form.size(class="form-control field", id="size") }}</td>
<td>{{ widgets.errors(form.size.errors) }}</td>
</tr>
</table>
</div>
</div>
尝试利用 Array.prototype.map()
, Array.prottotype.join()
var options = {
"ipe": ["80", "100"],
"hea": ["90", "110"]
};
$("#shape").change(function() {
var val = options[this.value];
$("#size").html(function() {
return val.map(function(el) {
return $("<option>", {
"name": el,
"value": el,
"html": el
})[0].outerHTML
}).join("");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<label>Shape:
<select id="shape">
<option>select an option</option>
<option name="ipe" value="ipe">ipe</option>
<option name="hea" value="hea">hea</option>
</select>
</label>
<label>Size:
<select id="size">
</select>
</label>
我找到了一个示例,它通过给定的 html 字符串填充 select 字段:
<script>
$(document).ready(function() {
$("#shape").change(function() {
var val = $(this).val();
$("#size").html(options[val]);
});
var options = [
"<option value='test'>item1: test 1</option><option value='test2'>item1: test 2</option>",
"<option value='test'>item2: test 1</option><option value='test2'>item2: test 2</option>",
"<option value='test'>item3: test 1</option><option value='test2'>item3: test 2</option>"
];
});
</script>
哪个工作正常,但我必须如何更改脚本才能对给定的字典执行相同的操作?
options = {
'ipe': ['80', '100'],
'hea': ['90', '110'],
}
这里用一张图来说明:
这是我的 html 片段:
<div class="panel panel-primary">
<div class="panel-body">
<table>
<tr>
<td>{{ form.shape.label }}</td>
<td>{{ form.shape(class="form-control field", id="shape") }}</td>
<td>{{ widgets.errors(form.shape.errors) }}</td>
</tr>
<tr>
<td>{{ form.size.label }}</td>
<td>{{ form.size(class="form-control field", id="size") }}</td>
<td>{{ widgets.errors(form.size.errors) }}</td>
</tr>
</table>
</div>
</div>
尝试利用 Array.prototype.map()
, Array.prottotype.join()
var options = {
"ipe": ["80", "100"],
"hea": ["90", "110"]
};
$("#shape").change(function() {
var val = options[this.value];
$("#size").html(function() {
return val.map(function(el) {
return $("<option>", {
"name": el,
"value": el,
"html": el
})[0].outerHTML
}).join("");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<label>Shape:
<select id="shape">
<option>select an option</option>
<option name="ipe" value="ipe">ipe</option>
<option name="hea" value="hea">hea</option>
</select>
</label>
<label>Size:
<select id="size">
</select>
</label>