简体   繁体   English

如果在下拉列表中选择了某个项目,如何创建文本框?

[英]How can I create a textbox if a certain item is selected in a drop-down list?

I have a drop-down list that contains the following values: 我有一个包含以下值的下拉列表:

<option>Aramex</option>
<option>DHL</option>
<option>FedEx</option>
<option>Other</option>

When the user select Other , I want to show a text box. 当用户选择Other ,我要显示一个文本框。 How can I do that? 我怎样才能做到这一点?

I'm using Laravel 4.2 我正在使用Laravel 4.2

One method: 一种方法:

<script type="text/javascript>
// Function to on change of the <select>, display the other input box
$(document).ready(function(){
    $("#choices").on("change"),function(){
    if($("#choices').val() == "Other"){
      $("#otherinput").show();
    }else{
      $("#otherinput").hide();
    }
   });
});
</script>

<!-- hide the input box initially -->
<style>
#otherinput{display: none;}
</style>
<!-- These must have values since they're options, FYI, but set an ID for select and add values to options -->
<select id='choices'>
<option value='Aramex'>Aramex</option>
<option value='DHL'>DHL</option>
<option value='FedEx'>FedEx</option>
<option value='Other'>Other</option>
</select>
<!-- hidden until you select other option within the <select> -->
<input type='text' name='textinput' id='otherinput' />

here is the correct fix: 这是正确的解决方法:

<script type="text/javascript">
    function showfield(name){
      if(name=='Other')document.getElementById('div1').innerHTML='Other: <input type="text" name="other" />';
      else document.getElementById('div1').innerHTML='';
    }
    </script>

    <select name="travel_arriveVia" id="travel_arriveVia" onchange="showfield(this.options[this.selectedIndex].value)">
    <option selected="selected">Please select ...</option>
    <option value="DHL">DHL</option>
    <option value="Aramex">Aramex</option>
    <option value="FedEx">FedEx</option>
    <option value="Other">Other</option>
    </select>
    <div id="div1"></div>

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

相关问题 如何从下拉列表中隐藏选定的选项 - How can I Hide Selected option from Drop-down list 返回页面时如何保持下拉列表的选定值 - How can I maintain selected value of drop-down list while come back to page 从数据库中警报选定的循环下拉列表项 - Alert selected drop-down list item of looping from database 如何使用bootstrap创建下拉菜单? - How can I create a drop-down menu using bootstrap? 如何使用取决于另一个下拉列表中所选选项的下拉列表显示表格简码 - how to display table shortcode with a drop-down list that depends on the selected option in another drop-down list 如何链接到选定的下拉项以打开网页 - How to Link to a Selected Drop-Down Item to Open a Web Page icCube下拉小部件-如何获取所选项目? - icCube drop-down widget - How to get Selected Item? 下拉菜单中获得所选项目的文本 - Drop-down get Selected item Text 如何使我的动态下拉列表也依赖于父下拉列表? - How can I make my dynamic drop-down list be dependent dependent also to parent drop down? Javascript-如何在AngularJS中创建一个默认选择一个国家/地区的国家和城市的级联下拉列表? - Javascript - How to create a cascading drop-down list of country & cities with one country selected as default in AngularJS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM