doupeng8494 2013-01-31 09:15
浏览 39
已采纳

根据下拉选择显示多个文本框

I want to display a number of textboxes in a form based on the selected option from a dropdown listbox.

For example, if user selects 1 then 1 textbox should be shown and if user selects 2 then 2 textboxes should be displayed. And I need to do it in PHP.

I found some answers using jQuery. Can we use jQuery inside PHP? If yes, then how?

Edit

@Edwin Alex This is how my select option looks like.

<h2><u>DEPENDENT DETAILS</u></h2><br />
            <table  border="1" style="border-style:dotted" width="100%" id="dependenttable">
            <tr><td>No of Dependent</td><td><select name="numDep" id="dropdown">
                <option value="">Please Select</option>
                <option value="0">0</option>
                <option value="1">1</option>
                <option value="2">2</option></select></td></tr>

            <tr id="textboxDiv"></tr>

At the end of file inside <> these I have written your code.

  • 写回答

2条回答 默认 最新

  • dshm8998473 2013-01-31 09:27
    关注

    You can use Jquery to get this. Try this,

    HTML :

    <tr><td>No of Dependent</td><td><select name="numDep" id="dropdown">
        <option value="">Please Select</option>
        <option value="0">0</option>
        <option value="1">1</option>
        <option value="2">2</option></select></td>
    </tr>
    
    <tr id="textboxDiv"></tr>
    

    Jquery :

    <script type="text/javascript">
    $(document).ready(function() {
        $("#dropdown").change(function() {
            var selVal = $(this).val();
            $("#textboxDiv").html('');
            if(selVal > 0) {
                for(var i = 1; i<= selVal; i++) {
                    $("#textboxDiv").append('<input type="text" name="textVal[]" value="" />');
                }
            }
        });
    });
    </script>
    

    Paste this code at the bottom of your page inside tag.

    Your select box ID should be dropdown.

    You need to have a div with an ID textboxDiv to place your generated textboxes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码