当表行动态增加时,从数据库动态加载下拉列表

时间:2017-08-09 05:51:23

标签: javascript php jquery

我有以下代码来增加表的行并加载下拉列表和文本框,但是从数据库加载了一个下拉列表。如果我增加行,则第二行无法从数据库加载下拉列表。请帮我解决这个问题。

<?php   require_once('inc/SQL.php'); ?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">$(document).ready(function(){
    $("#addCF").click(function(){
        $("#customFields").append('<tr valign="top"><th scope="row"><label for="customFieldName">Custom Field</label></th><td><input type="text" class="code" id="customFieldName" name="customFieldName[]" value="" placeholder="Input Name" /> &nbsp; <input type="text" class="code" id="customFieldValue" name="customFieldValue[]" value="" placeholder="Input Value" /> &nbsp; <select class="form-control" name="productid[]" id="productid"><option>Select</option><?php $sql_opt="SELECT id, product_name from ct_product where status='1'";$result_opt=mysqli_query($con,$sql_opt);$options="";while ($row_opt=mysqli_fetch_array($result_opt)) {$code=$row_opt["id"];$name_opt=$row_opt["product_name"];$options.="<OPTION value='$code'>".$name_opt;} echo $options; ?></option></select>  &nbsp; <a href="javascript:void(0);" class="remCF">Remove</a></td></tr>');
        $(".remCF").on('click',function(){
            $(this).parent().parent().remove();
        });
    });
});</script>

<table class="form-table" id="customFields">
    <tr valign="top">
        <th scope="row"><label for="customFieldName">Custom Field</label></th>
        <td>
            <input type="text" class="code" id="customFieldName" name="customFieldName[]" value="" placeholder="Input Name" /> &nbsp;
            <input type="text" class="code" id="customFieldValue" name="customFieldValue[]" value="" placeholder="Input Value" /> &nbsp;
            <select class="form-control" name="productid[]" id="productid">
                   <option>Select</option>
                    <?php           
                        $sql_opt="SELECT id, product_name from ct_product where status='1'";
                        $result_opt=mysqli_query($con,$sql_opt);
                        $options="";
                        while ($row_opt=mysqli_fetch_array($result_opt)) {
                            $code=$row_opt["id"];
                            $name_opt=$row_opt["product_name"];
                            $options.="<OPTION value='$code'>".$name_opt;}
                        ?>       
                    <?php echo $options; ?>
                    </option>

                  </select>
            <a href="javascript:void(0);" id="addCF">Add</a>
        </td>
    </tr>
</table>

0 个答案:

没有答案
相关问题