选择选项时,我想更改其他选择标签选项
在我的Django模板中,我有两个选择标签。我想更改第二个标签以链接到第一个标签。例如,当在第一个标签动物选项中选择时,我想在第二个选择标签上显示动物。当植物在第一个标签中选择时,我想在第二个选择标签中展示植物。我该怎么做?
(animals or plants)
<select id="creatures">
{% for x in list %}
<option value="{{x.creature}}">{{x.creature}}</option>
{% endfor %}
</select>
<select id="instances">
{% for y in instances %}
<option value="{{y.instance}}">{{x.instance}}</option>
{% endfor %}
</select>
In my Django template I have two select tag. I want to change the second tag to be linked to the first. For example when in first tag animals option select I want to show the animals on the second select tag. When plants select in first tag I want to show plants in the second select tag. How can I do this?
(animals or plants)
<select id="creatures">
{% for x in list %}
<option value="{{x.creature}}">{{x.creature}}</option>
{% endfor %}
</select>
<select id="instances">
{% for y in instances %}
<option value="{{y.instance}}">{{x.instance}}</option>
{% endfor %}
</select>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有多种方法。
这是您可以采用的一种方法:
/a>
我们使用
innerhtml
更新第二个选择的内容。另一种方法是在静态上写下不同的选择元素,但默认情况下隐藏。然后,您可以根据第一个选择中选择的选项来解开所需的选择元素。
希望它有帮助。
There are multiple ways.
Here is one approach you can take:
Try it in CodeSandbox
We're updating the contents of the second select using
innerHTML
.Another approach would be to have different select elements written statically but hidden by default. Then you can unhide the select element you want based on the option selected in the first select.
Hope it helps.