简体   繁体   中英

How ajax work in dynamically generated div

I am doing sales of product in which I use AJAX to capture data based on a selection from a dropdown to another one. When I dynamically create a field it works only once i want it to work N number of time.

Here is my HTML & PHP Code:

    <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>

Here is My Js Script to Call Ajax Each Time

<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>

Here is my Ajax File

    <?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>';
    }
}
?>

Whenever I create a new field dynamically my AJAX is not working.

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>

The comments made it clear that you are duplicating your form. I think you have duplicate IDs on your page and your jQuery event will not work anymore. An ID may only occur once.

Take a look here: Clone form and increment

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM