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]);
                }
            });
        });
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来