1

新手来了 我做了一个 ajax 来计算用户数并将其显示在我的徽章上,但是,每当我单击 1 个按钮(带表格的模式)时,它都会显示在所有徽章上。例如。我的第一行模式表中有 4 个用户。#4 计数显示在我的所有徽章上。这很奇怪。我的目标是动态显示我的徽章中的计数。我在下面提供了一个视频和图片,以便更好地解释。

https://streamable.com/z6dqgi << 这是我所做的输出。(查看徽章计数行为)

这就是我的目标/目标。 在此处输入图像描述

看法:

<div class="tab-content" id="custom-tabs-two-tabContent">
              
              <div class="tab-pane fade show active" id="custom-tabs-two-all" role="tabpanel" aria-labelledby="custom-tabs-two-all-tab">
                     <table class="table">
                            <thead class="">
                                <tr>
                                <th scope="col"><i class="fas fa-hashtag"></i></th>
                                <th scope="col"><i class="fas fa-mobile-alt mr-2"></i>UUID</th>
                                <th scope="col">Full Name</th>
                                 <th scope="col"><i class="fas fa-envelope-open-text mr-2"></i>Email</th>
                                <th scope="col"><i class="fas fa-phone-alt mr-2"></i>Contact Number</th>
                                <th scope="col"><i class="fas fa-at mr-2"></i>Username</th>
                                <th scope="col">Level</th>
                                <th scope="col">balance</th>
                                <th scope="col">&emsp;&emsp;&nbsp;Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                
                               <?php 
                               foreach($result as $rows) {   $uuid = $rows->uuid;
                               $userID = $rows->userID; ?>     
                                   <?php if($rows->uuid===$_SESSION['uid']): ?>     
                                <tr>
                                <th><?php echo $rows->userID;  ?></th>
                                <td><?php echo $rows->uuid; ?></td>
                                <td><?php echo $rows->firstname; ?>&nbsp;<?php echo $rows->lastname; ?></td>
                                <td><?php echo $rows->email; ?></td>
                                <td><?php echo $rows->mobile; ?></td>
                                <td><?php echo $rows->username; ?></td>
                                <td><?php echo $rows->account_type; ?></td>
                                <td><?php echo $rows->currentPoints; ?></td>
                                <td>
                                   
                                        <button data-id="<?php echo $rows->userID; ?>" class=" fundTable btn btn-success btn-sm text-bold " type="button"  data-toggle="modal" data-target="#fundModal">
                                            <i class="fas fa-hand-holding-usd mr-1"></i> <?php echo $rows->userID; ?>FUND
                                        </button>
                                        
                                        
                                        <button data-id="<?php echo $rows->userID; ?>" class=" allTable btn btn-danger btn-sm text-bold" type="button"  data-toggle="modal" data-target="#editModal">
                                            &nbsp;&nbsp;&nbsp;<i class="fas fa-users"></i>
                                            <span data-id="<?php echo $rows->userID; ?>" class="badge badge-warning navbar-badge"></span>
                                            
                                        </button>   
                                 
                                        
                                        
                                
                                        
                               </td>
                               </tr>
                                     <?php else: ?>
                                                  
                                            <?php endif;?>
                                     <?php } ?>
                            </tbody>
                     </table>
   </div>

<span class="badge badge-warning navbar-badge">2</span>  //this is where the total value should be displayed.                                           
</button> 

模型:

public function view($userID = 0){
            $this->db->select("*");
            $this->db->from("users");
            $this->db->where( "uuid=".$userID);
            $query = $this->db->get();
            return $result = $query->result();
            
        }

控制器:

public function view()
    {
        
        if ($this->input->is_ajax_request()) {
            $view_id = $this->input->post('view_id');
            
            if ($post = $this->networks->view($view_id)) {
                $data = array('responce' => 'success', 'post' => $post);
              
            } else {
                $data = array('responce' => 'error', 'message' => 'failed to fetch record');
            }
            echo json_encode($data);
        } else {
            echo "No direct script access allowed";
          
        }
        
    }

脚本:

<script>


          $(document).ready(function(){
    $(".allTable").on("click", function(){
        var view_id = $(this).data('id')
    
        $.ajax({
         
           url: "<?=site_url('network/view')?>",

          type: "post",
          dataType: "json",
          data: {
            view_id: view_id  
          },
            success: function(data){
              
              var tbody ="";
              var item =data.post; 


            for(var key in item) {
                    tbody +="<tr>";
                    tbody += "<td>"+item[key].userID+"</td>";
                    tbody += "<td>"+item[key].uuid+"</td>";
                    tbody += "<td>"+item[key].firstname+" "+item[key].lastname+"</td>";
                    tbody += "<td>"+item[key].email+"</td>";
                    tbody += "<td>"+item[key].mobile+"</td>";
                    tbody += "<td>"+item[key].username+"</td>";
                    tbody +="</tr>"
                                 }
           $(".navbar-badge").text(Object.keys(item).length); 
          $(".tbody").html(tbody);
          $('#editModal').modal('show');
            }
     
        });
        })
        });
 
</script>
4

1 回答 1

1

正如您所使用的那样.navbar-badge,它将针对具有该类的所有元素,这就是它更改所有跨度标签的原因。相反,当您已经获得data-id按钮时,您可以使用它来仅使用$("span[data-id=" + view_id + "]").text("sometext").

演示代码

$(document).ready(function() {
  $(".allTable").on("click", function() {
    var view_id = $(this).data('id')

    /*$.ajax({
//somecodes of ajx ..
      */
    // $("span[data-id="+view_id+"]").text(Object.keys(item).length);
    $("span[data-id=" + view_id + "]").text(10);
    // other codes ..
    /*  }
    });*/
  })
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.0/css/all.css">

<table class="table">
  <thead class="">
    <tr>
      <th scope="col"><i class="fas fa-hashtag"></i></th>
      <th scope="col"><i class="fas fa-mobile-alt mr-2"></i>UUID</th>
      <th scope="col">Full Name</th>
      <th scope="col"><i class="fas fa-envelope-open-text mr-2"></i>Email</th>
      <th scope="col"><i class="fas fa-phone-alt mr-2"></i>Contact Number</th>
      <th scope="col"><i class="fas fa-at mr-2"></i>Username</th>
      <th scope="col">Level</th>
      <th scope="col">balance</th>
      <th scope="col">&emsp;&emsp;&nbsp;Actions</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>
        1
      </th>
      <td>
        123
      </td>
      <td>
        abc
      </td>
      <td>
        a@gmail.com
      </td>
      <td>
        12213
      </td>

      <td>

        <button data-id="1" class=" fundTable btn btn-success btn-sm text-bold " type="button" data-toggle="modal" data-target="#fundModal">
                                            <i class="fas fa-hand-holding-usd mr-1"></i> FUND
                                        </button>


        <button data-id="1" class=" allTable btn btn-danger btn-sm text-bold" type="button" data-toggle="modal" data-target="#editModal">
                                            &nbsp;&nbsp;&nbsp;<i class="fas fa-users"></i>
                                            <span data-id="1" class="badge badge-warning navbar-badge"></span>
                                            
                                        </button>

      </td>
    </tr>
    <tr>
      <th>
        2
      </th>
      <td>
        1232
      </td>
      <td>
        abc2
      </td>
      <td>
        a2@gmail.com
      </td>
      <td>
        12213
      </td>

      <td>

        <button data-id="2" class=" fundTable btn btn-success btn-sm text-bold " type="button" data-toggle="modal" data-target="#fundModal">
                                            <i class="fas fa-hand-holding-usd mr-2"></i> FUND
                                        </button>


        <button data-id="2" class=" allTable btn btn-danger btn-sm text-bold" type="button" data-toggle="modal" data-target="#editModal">
                                            &nbsp;&nbsp;&nbsp;<i class="fas fa-users"></i>
                                            <span data-id="2" class="badge badge-warning navbar-badge"></span>
                                            
                                        </button>

      </td>
    </tr>

  </tbody>
</table>

于 2020-12-25T05:26:57.913 回答