ajax如何在动态生成的div中工作

时间:2018-04-06 05:33:54

标签: javascript php jquery ajax

我正在销售产品,我使用AJAX根据从下拉列表到另一个下拉列表的选择来捕获数据。当我动态创建一个字段时,它只能工作一次,我希望它能工作N次。

这是我的HTML& PHP代码:

    <div class="row col-md-12 mrg-top-20">
<h4 class="card-title" style="color:#ff0033">Sales Details</h4><br>


    <div class="col-md-12"></div>
    <div class="row col-md-12" data-duplicate="add">

        <div class="col-md-4">
        <label>Select <strong>Installation Date</strong></label>
                <div class="form-group">
                    <div class="timepicker-input input-icon form-group">
                        <i class="ti-time"></i>
                        <input type="text" name="ins_date" class="form-control datepicker-1" placeholder="Datepicker" data-provide="datepicker">
                    </div>  
                </div>
        </div>
        <div class="col-md-4" id="sel_dynamic">
                <div class="form-group">
                <label>Select <strong>Manufacturer</strong></label>
                <select name="manu" id="manu" class="form-control" style="width:100%"> 
                    <option disabled="disabled" selected="selected">Select Manufacturer</option>
                    <?php
                        $data = mysqli_query($conn,"SELECT * FROM manufacturer_details");
                        while($itemcat = mysqli_fetch_array($data))
                        {
                    ?>
                    <option value="<?php echo $itemcat['MANUFACTURER_ID']; ?>"><?php echo $itemcat['MANUFACTURER_NAME'];?></option>
                    <?php
                        }
                    ?>
                </select>
                </div>
        </div>

        <div class="col-md-4">
            <div class="form-group">
                <label>Select <strong>Modal</strong></label>
                <select name="modal" id="modal" class="form-control" style="width:100%"> 
                    <option value="" selected disabled="disabled">Select Modal...</option>

                </select>
            </div>
        </div>

        <div class="col-md-4">  
            <div class="form-group">
                <label>Ser<strong>ial</strong></label>
                <input type="text" name="serial" placeholder="Enter Serial No" class="form-control">
            </div>
        </div>


        <div class="col-md-4">
            <label>Select <strong>AMC Start Date</strong></label>
            <div class="form-group">
                <div class="timepicker-input input-icon form-group">
                    <i class="ti-time"></i>
                    <input type="text" name="amc_date" class="form-control datepicker-1" placeholder="Datepicker" data-provide="datepicker" data-date-format="mm/dd/yyyy">
                </div>  
            </div>
        </div>


        <div class="col-md-4">
            <label>Select <strong>AMC End Date</strong></label>
            <div class="form-group">
                <div class="timepicker-input input-icon form-group">
                    <i class="ti-time"></i>
                    <input type="text" name="amc_edate" class="form-control datepicker-1" placeholder="Datepicker" data-provide="datepicker" data-date-format="mm/dd/yyyy">
                </div>  
            </div>
        </div>  

    </div>  

    <div>
        <input type="button" id="add" data-duplicate-add="add" class="btn btn-info" value="+"/>
    </div>
    <div >
        <input type="button"  data-duplicate-remove="add" class="btn btn-info" value="-"/>
    </div>
</div>

以下是每次调用Ajax的My Js脚本

<script type="text/javascript">
    $(document).ready(function()
    {
        $("#sel_dynamic").on("change","#manu",(function() //just try to use on change
        {
            var id = $("#manu").val();
            var dataString = 'id='+ id;
            $.ajax
            ({
                type: "POST",
                url: "ajax_modal.php",
                data: dataString,
                cache: false,
                success: function(html)
                {
                    $("#modal").html(html);
                } 
            });
        });
    });
</script>

这是我的Ajax文件

    <?php
include('script/db.php');
if($_POST['id'])
{
    $id=$_POST['id'];
    $sql=mysqli_query($conn,"select * from modal_details where MANUFACTURER_ID='".$id."'");
?>

    <option selected disabled="disabled">Select Modal Name</option>
<?php
    while($row = mysqli_fetch_assoc($sql))
    {
        $id = $row['MODAL_ID'];
        $data = $row['MODAL_NAME'];
        echo '<option value="'.$id.'">'.$data.'</option>';
    }
}
?>

每当我动态创建一个新字段时,我的AJAX就无法正常工作。

3 个答案:

答案 0 :(得分:1)

Try this :

<script type="text/javascript">
    $(document).ready(function()
    {
        $(document).on('change','#manu',function()
        {
            var id = $("#manu").val();
            var dataString = 'id='+ id;
            $.ajax
            ({
                type: "POST",
                url: "ajax_modal.php",
                data: dataString,
                cache: false,
                success: function(html)
                {
                    $("#modal").html(html);
                } 
            });
        });
    });
</script>

答案 1 :(得分:1)

评论清楚表明您正在复制表单。我认为您的页面上有重复的ID,您的jQuery事件将不再起作用。 ID只能出现一次。

看看这里: Clone form and increment

答案 2 :(得分:0)

Add the class to select as shown below: 

<select name="manu" id="manu" class="form-control manuClass" style="width:100%">

And use the below script

<script type="text/javascript">
    $(document).ready(function()
    {
        $(document).on('change','.manuClass',function()
        {
            var id = $(this).val();
            var dataString = 'id='+ id;
            $.ajax
            ({
                type: "POST",
                url: "ajax_modal.php",
                data: dataString,
                cache: false,
                success: function(html)
                {
                    $(this).parent().parent().next().children().children("#modal").html(html);
                } 
            });
        });
    });
</script>