【发布时间】:2022-02-10 16:52:40
【问题描述】:
情况是这样的
我的 html 代码中有一个选择元素,有很多选项。这些选项之一是“其他”。我想要的是,当我选择“其他”时,在不刷新页面的情况下,在带有 JS 的选择项下方显示一个输入元素。问题是我必须刷新页面才能看到更改。你能帮助我吗? :)
这是我的代码:
<label for="select-tribunal" class="form-label">Cadre Légal</label>
<select id="select-tribunal" required class="form-select" aria-label="Default select example">
<?php
foreach ($tribunaux as $tribunal){
echo '<option value="'.$tribunal['id'].'">'.$tribunal['nom'].'</option>';
}
?>
<option value="0">Autre Tribunal</option>
</select>
<input type="text" class="form-control" id="tribunal" style="visibility: hidden" placeholder="Entrez le nom du tribunal">
<script>
var tribunal = document.getElementById("select-tribunal");
if (tribunal.options[tribunal.selectedIndex].value === '0') {
document.getElementById('tribunal').style.visibility = 'visible';
} else {
document.getElementById('tribunal').style.visibility = 'hidden';
}
</script>
提前致谢:)
【问题讨论】:
标签: javascript html dynamic html-select