doubu5035 2017-04-04 12:01 采纳率: 100%
浏览 22
已采纳

下拉选择的新ajax请求

I have three tables and I'm displaying each one of them using a select list element and have ajax and jquery to return the tables. I want whenever I choose a new table from the list ajax should bring me data only from that table.

Here is my form:

<div class="panel">
                <div class="panel-heading">
                    <div class="form-group">
                        <div class="input-group">
                            <span class="input-group-addon">Term</span>
                            <!-- I passed the tables name as values for a specific period -->
                            <select name="term" class="form-control" id="term">
                                <option value="">Select Period</option>
                                <option value="period_one">1st Period</option>
                                <option value="period_two">2nd Period</option>
                            </select>
                        </div>
                </div>
                <div class="panel-body">
                    <table class="table table-responsive table-bordered table-condensed table-striped table-hover" id="dataTable">
                        <thead>
                            <tr>
                                <th>Student Name</th>
                                <th>Subject</th>
                                <th>Class</th>
                                <th>Score</th>
                            </tr>
                        </thead>
                        <tbody id="periodTable">

                        </tbody>
                    </table>
                </div>
            </div>

The problem is not that I'm not getting the desired result. I'm getting the result that I want that's why I feel that there's no need for me to add codes from findGrades.php.

here is my script:

$(document).ready(function() {
$('#term').on('change', function() {
  /* Act on the event */
    var term = $('#term').val();

    if (term != '') {
      $.ajax({
        url:"findGrades.php",
        type:"post",
        data:{"term":term},
        dataType:"json",
        success:function(data){
            // Lfrankie solution
            $("#periodTable").replaceWith('<tbody id="periodTable"></tbody>');

           for (var count = 0; count < data.length; count++) {
                var htmlData = '<tr><td data-type="text" data-name="student_name" data-pk="'+data[count].id+'" class="student_name">'+data[count].first_name+' '+data[count].middle_name+' '+data[count].surname+'</td>';

                htmlData += '<td data-type="text" data-name="subject_name" data-pk="'+data[count].id+'" class="subject_name">'+data[count].subject_name+'</td>';

                htmlData += '<td data-type="text" data-name="class_name" data-pk="'+data[count].id+'" class="class_name">'+data[count].class_name+'</td>';

                if (data[count].score <= 69 ) {
                    htmlData += '<td style="color:red;" data-type="text" data-name="score" data-pk="'+data[count].id+'" class="score">'+data[count].score+'</td></tr>';
                } else {
                    htmlData += '<td data-type="text" data-name="score" data-pk="'+data[count].id+'" class="score">'+data[count].score+'</td></tr>';
                }

               //I feel this is where my problem lies
               $("#periodTable").append(htmlData);

            }
            $('#dataTable').DataTable();
        }
      });
    } else {
      $("#periodTable").html('');

    }

 });
});

The problem I'm having is whenever I select a table it brings the result fine, but when I select a new table it adds the new result to the old result.

Ex: table 1:

  1. cat
  2. rat

table 2 1. cat 2. rat 3. dog 3. goat

Whereas result 2 should only show 'dog' and 'goat'. I realize it's because of the way I'm adding data $("#periodTable").append(htmlData);. I have tried changing it to this $("#periodTable").html(htmlData); which only returns a single item from a table like 'cat' from table 1 and 'rat' from table 2

How can I effectively get this working. Let me know if more information is needed.

Update: Just added my html as requested by some. Also I included a essential aspect I forget to mention 'Datables', which I added also.

  • 写回答

3条回答 默认 最新

  • douzhong4222 2017-04-04 12:13
    关注

    Without seeing the HTML for your page, I can only guess that you start with an empty table. Your first AJAX call appends the first set of data. Your second call appends its data, because that's what you've asked it to do.

    To fix this, you have to replace the empty table at the start of your success() function. You can then safely append the new data, whether it's the first time you call it or the n-th time.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)