隐藏在HTML中选择选项时的项目。

huangapple go评论58阅读模式
英文:

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';
}

huangapple
  • 本文由 发表于 2023年7月14日 00:59:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76681754.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定