dscbxou1900343 2013-10-09 16:24
浏览 29

从下拉列表中选择选项后如何创建文本框?

I'm trying to create a drop-down list with four options such that if I select the 4th option, I want a text box created so that I can get the value typed in that box using "$_GET"

Something like this;

<select name="value">
<option value="value1">Option 1</option>
<option value="value2">Option 2</option>
<option value="value3">Option 3</option>
<!-- And a 4th one -->
</select>

And if the 4th one is selected, a box should appear like this;

<input type="text" name="firstname">

Edit;

My current code;

<script>
jQuery(function($){                      //calling the code inside braces when document loads and passing jQuery object as '$' parameter;
   $("select[name='sortby']").change(function(){        //binding an event that fires every time select value changes
      var select = $(this);              //caching select, which value was changed
      if(select.val() === "byDefindex"){     //checking if we selected the right option
          $("<input>").attr({type: "text", name: "defindex"}).appendTo(select.parent());   //creating new input element object, setting its value to "value4" and appending to select parent element or wherever you want it
      }
   });        
});
</script>




<form action="<?php $_PHP_SELF ?>" method="GET">

    Select:
    <br />
        <select name="sortby">
            <option value="playHours">Play Hours</option>
            <option value="lastLogin">Last Login</option>
            <option value="byDefindex">By Defindex</option>
        </select>
    <br />

    <input type="submit" />

</form>
  • 写回答

3条回答 默认 最新

  • dongxing2015 2013-10-09 16:32
    关注

    If your 4th option is this:

    <option value="value4">Option 4</option>
    

    You can use jQuery to display the field. Put your field in a <div>

    <div id="field"><input type="text" name="firstname"></div>
    

    Now,

    <script type="text/javascript">
    $(document).ready(function(){
    
    $('input[name="value"]').change(function(){
    var v = $('input[name="value"]').val();
    if(v=="value4") $('#field').show();
    else $('#field').hide();
    })
    })
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。