简体   繁体   English

切换 HTML 选择框值时如何隐藏/显示 HTML 输入框?

[英]How do I hide/show HTML input boxes when I toggle HTML Select box values?

Assume there is a form with the following three fields:假设有一个包含以下三个字段的表单:

  • HTML Select box with two values (Metric/English)带有两个值的 HTML 选择框(公制/英制)
  • HTML Input box with label value_metric带有标签value_metric HTML 输入框
  • HTML Input box with label value_english带有标签value_english HTML 输入框

For end-user I want only the two fields shown, the select box, and the corresponding metric or English Input box.对于最终用户,我只需要显示两个字段、选择框和相应的指标或英语输入框。 Toggling the select box shows/hides the corresponding input boxes.切换选择框显示/隐藏相应的输入框。

How do I do this?我该怎么做呢? How do I hide one while showing the other?如何在显示另一个的同时隐藏一个? I am looking for the method of operation, not necessarily the actual code.我在寻找操作方法,不一定是实际代码。

For example, do I hide it, disable it, make it read only, do something else?例如,我是否将其隐藏、禁用、只读、执行其他操作? I want it to seamlessly update and swap when I toggle the Select box.我希望它在我切换选择框时无缝更新和交换。

 $('select').change(function(){ if($(this).val()==="metric"){ $('.metric_val').show(); $('.english_val').hide(); }else{ $('.english_val').show(); $('.metric_val').hide(); } }).change();
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select name="type"> <option value="metric">Metric</option> <option value="english">English</option> </select> <input class="metric_val" type="text" name="metric_val" placeholder="METRIC"> <input class="english_val" type="text" name="english_val" placeholder="ENGLISH">

Running example: Hope this helps运行示例:希望这有帮助

 function myFunction(){ var _select_box_val = document.getElementById("select_box").value; if("Metric"==_select_box_val){ document.getElementById("value_metric").style.display = "block"; document.getElementById("value_english").style.display = "none"; }else if("English"==_select_box_val){ document.getElementById("value_english").style.display = "block"; document.getElementById("value_metric").style.display = "none"; } } myFunction();
 .hide{ display:none; }
 <select id="select_box" onchange="myFunction()"> <option>Metric</option> <option>English</option> </select> <input type="text" placeholder="value_metric" id="value_metric" class="hide"> <input type="text" placeholder="value_english" id="value_english" class="hide">

You can use javascript to hide and show the html input boxes based on the select box.您可以使用 javascript 来隐藏和显示基于选择框的 html 输入框。 Add a Eventlistener to the selectbox which in turn can call functions such as,将事件侦听器添加到选择框,它又可以调用函数,例如,

function toggleMetric(){
    var metricBox = document.getElementbyID('value_metric');
    var englishBox = document.getElementbyID('value_english');    

    metricBox.style.display = 'block';
    englishBox.style.display = 'none';
}

Then with that value you can then change the style of the input box to be display block or none depending if you want to show it or not.然后使用该值,您可以将输入框的样式更改为显示块或不显示,具体取决于您是否要显示它。 For example,例如,

$('select').change(function() {
       if($(this).val() == 'Metric') {
         $('#Metric').show();
         $('#English').hide();
       }else{
         $('#English').show();
         $('#Metric').hide();
       }
  });

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 <select>
    <option value="Metric">Metric</option>
    <option value="English">English</option>
 </select>
<input type="text" name="Metric" id="Metric">
<input type="text" name="English" id="English" style="display: none;">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM