weixin_33694620 2019-02-10 11:22 采纳率: 0%
浏览 15

AJAX调用过多

My code is so messy. I create 1 Ajax call everytime I display an element. My table will have a fixed number of 80 rows. How can I simplify my repetitive AJAX calls.

HTML

<tr>
<td>
    <div class="form-group row mb-0" >
        <div class="col-12">
            <select class="form-control form-control-sm mySelect2 " name="111" id="111">
                <?php
                include ('server_side/connection.php');
                $sql = "SELECT * FROM tbl_subjects";
                $result=mysqli_query($conn, $sql);
                while ($row=mysqli_fetch_array($result)) { ?>
                <option value="<?php echo $row['subject_code'];?>"><?php echo $row['subject_code'];?></option>
                <?php } ?>
            </select>
        </div>
    </div>
</td>
<td><span id="description_111"></span></td>
<td><span id="prerequisite_111"></span></td>
<td><span id="unit_111"></span></td>

AJAX

$("#111").change(function(){
        var subject_code = $(this).val();
        $.ajax({
            type: "POST",
            url: "server_side/load_subjectdesc.php",
            data: {subject_code: subject_code},
            success: function(result){
            $("#description_111").html(result);
            }
        });
    });
    $("#111").change(function(){
        var subject_code = $(this).val();
        $.ajax({
            type: "POST",
            url: "server_side/load_subjectpreq.php",
            data: {subject_code: subject_code},
            success: function(result){
            $("#prerequisite_111").html(result);
            }
        });
    });
    $("#111").change(function(){
        var subject_code = $(this).val();
        $.ajax({
            type: "POST",
            url: "server_side/load_subjectunit.php",
            data: {subject_code: subject_code},
            success: function(result){
            $("#unit_111").html(result);
            }
        });
    });

One Ajax per One item to be displayed is too much. How can I simplify or remove so many AJAX calls?

  • 写回答

1条回答 默认 最新

  • weixin_33730836 2019-02-10 11:53
    关注

    You will need to create a php page which will return a JSON of this form:

    {
        description: 'descriptionvalue',
        perequisite: 'perequisitevalue',
        unit: 'unitvalue'
    }
    

    and you would send an AJAX request to it, like:

    $("#111").change(function(){
        var subject_code = $(this).val();
        $.ajax({
            type: "POST",
            url: "server_side/yourpage.php",
            data: {subject_code: subject_code},
            success: function(result){
                for (var key in result) $("#" + key + "_111").html(result[key]);
            }
        });
    });
    

    You could refine this further:

    function myChange(idVal) {
        $("#" + idVal).change(function(){
            var subject_code = $(this).val();
            $.ajax({
                type: "POST",
                url: "server_side/yourpage.php",
                data: {subject_code: subject_code},
                success: function(result){
                    for (var key in result) $("#" + key + "_" + idVal).html(result[key]);
                }
            });
        });
    }
    
    评论

报告相同问题?