drbi19093 2017-11-28 09:37
浏览 100

防止jquery在表中追加重复值

I am getting duplicate values while calling JSON response from AJAX using jQuery and PHP. The code is running perfectly but it is giving duplicate entries while selecting drop down list. There are two drop down list first one calls from db and based on first one the values get propagated in the second one. And while selecting selecting second one it will call a java script which Return JSON response from AJAX using jQuery but in a duplicated value.

$(document).ready(function(){
    $("#employee").change(function() {    
        //var id = $(this).find(":selected").val();
        //var dataString = 'empid='+ id; 
        var b_code = $(this).find(":selected").val();
        var dataString = 'empid='+ b_code;
    $.ajax({
        url: 'getData.php',
        type: 'get',
        dataType: 'JSON',
        data: dataString,
        success: function(response){
            var len = response.length;
            for(var i=0; i<len; i++){
                var id = response[i].id;
                var X1 = response[i].X1;
                var X2 = response[i].X2;
                var X3 = response[i].X3;
                        var X4 = response[i].X4;
                        var X5 = response[i].X5;
                
                var tr_str = "<tr>" +
                    "<td align='center'>" + (i+1) + "</td>" +
                    "<td align='center'>" + X1 + "</td>" +
                    "<td align='center'>" + X2 + "</td>" +
                    "<td align='center'>" + X3+ "</td>" +
                            "<td align='center'>" + X4 + "</td>" +
                              "<td align='center'>" + X5+ "</td>" +
                    "</tr>";
                    
                $("#userTable").append(tr_str);
            }
            } 
        })
    });
});
<!DOCTYPE html>
<html>

<head>
  <title>TSP</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!--<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" type="text/css" /> -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" type="text/javascript"></script>
 <link rel="stylesheet" href="csss/bootstrap.min.css">
 <link rel="stylesheet" href="csss/style1.css">
  <!--</style> -->
<script type="text/javascript">
           $(document).ready(function(){              
               $("#district").change(function(){
                     var district=$("#district").val();
                     $.ajax({
                        type:"post",
                        url:"getblock.php",
                        data:"district="+district,
                        success:function(data){
                              $("#employee").html(data);
                        }
                     });
               });
           });
      </script>
</head>

<body>
  <div class="container" style="margin-top: 50px;"> 
    <div class="col-sm-6 col-sm-offset-3"> 
      <div class="panel panel-primary">
        <div class="panel-heading">
          <h3 class="panel-title"><center>Select District & Block</center></h3>
        </div>
        <div class="panel-body">
        District :
        <select name="district" id="district">
          <option>-select your district-</option>
        <?php 
        include "db_connect.php"; 
        //$result=mysqli_query("SELECT DTCode,DTName from district order by DTName");
        $sql = "SELECT DTCode,DTName from district order by DTName";
        $resultset = mysqli_query($conn, $sql) or die("database error:". mysqli_error($conn));
        while($district = mysqli_fetch_assoc($resultset)){
        echo "<option value=$district[DTCode]>$district[DTName]</option>";
        } ?>
        </select>
 
        Block :
        <select name="block" id="employee">
            <option>-select your block-</option>
        </select>
        </div>
      </div>
   </div>
  </div> 
  
    
<div class="container">
   <table id="userTable" border="2" >
      <tr>
       <th width="5%">S.no</th>
       <th width="10%">X1</th>
       <th width="10%">X2</th>
       <th width="10%">X3</th>
       <th width="10%">X4</th>
       <th width="6%">X5</th>
      </tr>
   </table>
</div>

</body>

</html>
<script type="text/javascript" src="script/getDatan.js"></script>

</div>
  • 写回答

1条回答 默认 最新

  • douwudie8060 2017-11-28 10:05
    关注

    As I mentioned in my comment, you could use an array which stores the ids that you have passed, and on each iteration you can check whether you have a duplicate one.

    Here is an example:

    var testArray = [];
    
    var len = response.length;
        for(var i=0; i<len; i++){
            if($.inArray(response[i].id, testArray)){ // check whether you have went over it already
                continue;
            }
            testArray.push(response[i].id); // add the current id to the list and thats how you will avoid duplicates, assuming that the IDs are duplicate
            var id = response[i].id;
            var X1 = response[i].X1;
            var X2 = response[i].X2;
            var X3 = response[i].X3;
            var X4 = response[i].X4;
            var X5 = response[i].X5;
    
            var tr_str = "<tr>" +
                    "<td align='center'>" + (i+1) + "</td>" +
                    "<td align='center'>" + X1 + "</td>" +
                    "<td align='center'>" + X2 + "</td>" +
                    "<td align='center'>" + X3+ "</td>" +
                            "<td align='center'>" + X4 + "</td>" +
                              "<td align='center'>" + X5+ "</td>" +
                    "</tr>";
    
            $("#userTable").append(tr_str);
    

    Hope this helps!

    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R