英文:
Hide items when selecting options in HTML
问题
我有这个 select
元素:
<select class="form-control" name="inputContracts" id="inputContracts" required>
<option value="0">@lang('general.example.select')</option>
<option value="1">@lang('general.example.value1')</option>
<option value="2">@lang('general.example.value2')</option>
</select>
我想在选择选项时隐藏一些输入框,我应该怎么做?
我认为你可以用JavaScript来实现,但我不知道怎么做,你能给我一个思路吗?
英文:
I have this select
element:
<select class="form-control" name="inputContracts" id="inputContracts" required>
<option value="0">@lang('general.example.select')</option>
<option value="1">@lang('general.example.value1')</option>
<option value="2">@lang('general.example.value2')</option>
</select>
I want to hide some inputs when selecting an option, how can I do it?
I think you can do it with JavaScript, but I don't know how, can you give me an idea?
答案1
得分: 1
你可以按如下方式在 JavaScript 中获取所选值:
var e = document.getElementById("inputContracts");
var value = e.value;
一旦获取了值,你可以根据条件隐藏所需的元素。例如,如果你想在所选值为 "1" 时隐藏具有 id 为 "inputCont1" 的某些输入,可以按如下方式操作:
if(value == "1") {
document.getElementById("inputCont1").style.display = 'none';
}
英文:
You can get the selected value in javascript as follows:
var e = document.getElementById("inputContracts");
var value = e.value;
Once the value is obtained, you can hide the required element based on if conditon. For example if you want to hide some input whose id is inputCont1 when the value selected is "1", you can do as follows:
if(value == "1") {
document.getElementById("inputCont1").style.display = 'none';
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论