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 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)
  • ¥66 比特币地址如何生成taproot地址
  • ¥20 数学建模数学建模需要
  • ¥15 关于#lua#的问题,请各位专家解答!
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段