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

时间:2017-05-17 18:47:23

标签: javascript php jquery laravel-4 aramex

我有一个包含以下值的下拉列表:

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

当用户选择Other时,我想显示一个文本框。我怎么能这样做?

我正在使用Laravel 4.2

2 个答案:

答案 0 :(得分:2)

一种方法:

<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' />

答案 1 :(得分:0)

这是正确的修复:

<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>