duanjianao0592 2017-08-15 07:52
浏览 69
已采纳

如何在Jquery中向HTML表的每一行添加动态列表?

I have a dynamic table in a form, where depending on the user Input rows will be added to the table. Table column have Id, Name, Types of games(Dynamic list fetched from DB Table).

Now when user enters any number in No. of Students field and click on Load Games button, I am expecting that table should expand to that many rows so that user can insert the data in fields.

Its working as well but the problem is third field of the table is containing a list. and I am unable to understand how to populate the list for game_id field in every row of the table.

Code

                <?php
                $db =  new PDO("sqlite:c:/sqlite/games.db");  
                $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                $query_game = $db->prepare("select distinct games as di from outdoor_games;");
                $query_game->execute();
                $data = $query_game->fetchAll(); 

            ?>
            <!DOCTYPE html>
            <html>
             <head>
                <title>Access form</title>
                <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>   
                <script language="JavaScript">  </script>
             </head>
             <body>
                <form action="<?php $_SERVER['PHP_SELF'];?>"  method="post">
                <h2 style="text-align:center;">Test Form</h2>

                <fieldset style="width: 1000px;">

                          <legend><b> Source Profile</b></legend>

                            <TABLE id="STable" width="350px" border="1">
                            <tr>
                                <th bgcolor="WHITESMOKE">Student_ide</th>
                                <th bgcolor="WHITESMOKE">Student_Name</th>
                                <th bgcolor="WHITESMOKE">Game Id </th>
                            </tr>
                            <TR>
                                <td><input type= "text" name= "Stu_Id[]" value=1 /> </td>
                                 <td><input type= "text" name= "Stu_Name[]"  /> </td>
                                 <td><select name= "Game_id[]" /> 
                                        <option value=""><b>Games</b></option>
                                            <?php foreach ($data as $row){echo '<option value="' . $row['di'] . '">' . $row ['di'] . '</option>';} ?>
                                    </select>
                            </tr>    
                            </td>
                            </TR></TABLE></br> 
                            <label>No. of Students <input type= "text" id="s_cnt" name= "d_Count"></label>
                            <button type="button" class='loadgames' >Load Games</button>
                            <input type="submit" name ="add_SP" value ="Add Student Info" style="float: right;" /> </br> </br>

                 </fieldset ></br> </br>
                 <input type="submit" name ="exit" value ="EXIT"  />

            </form>
            <script>   

             $(".loadgames").on('click',function(){
                        var num = $("#s_cnt").val();
                        $("#STable tr").slice(2).remove();
                        for (var i = 2; i <= num; i++) 
                        {
                            var data = "<tr><td><input type='text' id='Stu_Id"+i+"' class='Stu_Id' name='Stu_Id[]' value = "+i+"  /></td><td><input type='text' id='Stu_Name"+i+"' name='Stu_Name[]'  /></td> <td><select name= 'Game_id[]' /><option value=""><b>Games</b></option>"+<?php foreach ($data as $row){echo '<option value="' . $row['di'] . '">' . $row ['di'] . '</option>';} ?>+"</select></tr>";
                            $("#STable").append(data);
                        }
                });

            </script>
             </body>
            </html>
  • 写回答

1条回答 默认 最新

  • duanguzhong5776 2017-08-16 10:54
    关注

    I could achieve the same with following jquery code :

     $(".loadgames").on('click',function()
      {
            var num = $("#s_cnt").val();
            $("#STable tr").slice(2).remove();
            for (var i = 2; i <= num; i++) 
            {
                    var data = "<tr><td><input type='text' id='Stu_Id"+i+"' class='Stu_Id' name='Stu_Id[]' value = "+i+"></td><td><input type='text' id='Stu_Name"+i+"' name='Stu_Name[]'/></td><td><select name='Game_id[]'><option value='null'>Games</option><?php foreach ($data as $row){echo "<option value=".$row['di'] . ">" .$row['di']. "</option>";}?></select></td></tr>";
                    $("#STable").append(data);
            }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应