duaj39673 2014-12-12 15:38
浏览 40

使用jquery / ajax在包含mysql数据的selectbox中动态添加行

I use this working code to add dynamically a row to a table with id "tableGenre" in the index.html file.

file -> script.js

    $( document ).ready(function() {  

        var scntDiv = $('#tableGenre');
        var i = $('#tableGenre tr').size() + 1;


        $('#addLine').on('click', function() {
                $('<tr><td>'+(i-1)+'</td><td><select name="genre[]" id="genre'+(i-1)+'"></select></td><td><a href="#" id="delLine" class="btn btn-xs red" ><i class="icon-remove"></i></a></td></tr>').appendTo(scntDiv);
                    alert(i);

                i++;
                return false;
        }); 

});

I would like to be able to add a select box populated by mysql data in the same row inserted dynamically.

Each time the user presses the button "AddLine" should display a new row with a select box containing the list of options taken from the mysql database through the file "buildGenre.php". However, the new line is created, but the select box remains empty

I tried so but without success

file -> script.js (new)

    $( document ).ready(function() {  

        var scntDiv = $('#tableGenre');
        var i = $('#tableGenre tr').size() + 1;


        $('#addLine').on('click', function() {
                $('<tr><td>'+(i-1)+'</td><td><select name="genre[]" id="genre'+(i-1)+'"></select></td><td><a href="#" id="delLine" class="btn btn-xs red" ><i class="icon-remove"></i></a></td></tr>').appendTo(scntDiv);
                    alert(i);

                    // If element id "genre1" exist

                    if($("#genre"+(i-1)).length != 0) {
                        //alert("#genre"+(i-1)+" exist");

                        // i populate select box options with mysql data

                        $.ajax({    
                            type: "GET",
                            url: "buildGenre.php",             
                            dataType: "html",   //expect html to be returned                
                            success: function(response){                    
                                $("#genre"+(i-1)).html(response); 
                                alert(response);
                            }

                        });

                    }
                i++;
                return false;
        }); 

});

file -> buildGenre.php

    $listGenri = '';
$sql = "SELECT * FROM tbl_genre";
$result = mysql_query($sql);
if(mysql_num_rows($result)>0){

    $listGenri .='';
    while($row = mysql_fetch_assoc($result)) {
        extract($row);
        $listGenri .= '<option value="'.$genre_id.'">'.$genre_name.'</option>';
    }

}
echo $listGenri;

file -> index.html

    <!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">

    <title>example</title>

    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script type="text/javascript" src="script.js"></script>

</head>

<body>
<input name="btnAdd" type="button" id="addLine" value=" New Line " >
<table id="tableGenre" border="1">
    <thead>
        <tr>
            <th>N.</th>
            <th>Genre</th>
            <th>Delete</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>

</body>
</html>

Thanks in advance!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算