切换类在 jquery 中无法正常工作


   <table class="table table-striped table-bordered table-hover" id="sample_2">
    <thead>
        <tr style="background:#52abe0; color:#fff;"> 
            <th>First Name</th>
            <th>LastName</th>
            <th>CIN </th>
            <th>Sexe</th>                             
            <th>Category</th>                              
            <th>City</th>                
            <th>Action</th>
         </tr>
    </thead>
    <tbody>
    <?php while($resulat = $sql_adherent->fetch_object()): ?>
         <tr class="odd gradeX">        
            <td><?php echo $resulat->fname_adherent;?></td>
            <td><?php echo $resulat->lname_adherent;?></td>
            <td><?php echo $resulat->cin;?></td>
            <td><?php echo $resulat->sexe;?></td>
            <td><?php echo $resulat->category;?></td>                              
            <td><?php echo $resulat->city;?></td>
            <td>
                <?php
                     $sql_licence = $mysqli->query("SELECT id_adherent FROM licences_clubs WHERE id_adherent='".$resulat->id_adherent."'"); 
                ?>
                <?php if($sql_licence->num_rows == 0){ ?>
                     <a href="#" rel='<?php echo $resulat->id_adherent; ?>' fed="<?php echo $resulat->id_federation; ?>" ligue="<?php echo $resulat->id_ligue; ?>" club="<?php echo $resulat->id_club; ?>" nom='<?php echo $resulat->fname_adherent; ?>' prenom='<?php echo $resulat->lname_adherent; ?>' id="validate" class="btn default btn-xs red-stripe">Validate</a>
               <?php }else{ ?>
                     <a href="#" rel='<?php echo $resulat->id_adherent; ?>' fed="<?php echo $resulat->id_federation; ?>" ligue="<?php echo $resulat->id_ligue; ?>" club="<?php echo $resulat->id_club; ?>" nom='<?php echo $resulat->fname_adherent; ?>' prenom='<?php echo $resulat->lname_adherent; ?>' id='validate' class="btn default btn-xs green-stripe"><i class="icon-check"></i></a>
               <?php } ?>
            </td>            
         </tr>
              <?php endwhile; ?>                            
             </tbody>
          </table>

我想切换类"绿条纹"one_answers"红条纹",这取决于数据库表数据,当我点击id="验证",我使用下面的代码。

下面是代码:
     $(document).ready(function(){
        $("#validate").on('click', function(e){
              e.preventDefault();
              var nom     = $(this).attr('nom'),
              prenom      = $(this).attr('prenom'),
              id_adherent = $(this).attr('rel'),    
              id_club     = $(this).attr('club'), 
              id_ligue    = $(this).attr('ligue'), 
              id_fed      = $(this).attr('fed'), 
  if($(this).hasClass('red-stripe')){
        $(this).removeClass('red-stripe');
        $(this).addClass('green-stripe');        
        $(this).html('<i class="icon-check">');
        $.ajax({ 
               url: 'ajax/demande_couverture.php', 
               data: 'id_adherent=' + id_adherent  + '&id_fed=' + id_fed + '&id_ligue=' + id_ligue + '&id_club=' + id_club + '&nom=' + nom + '&prenom=' + prenom,   
               beforeSend: function(){
                   $("#loading").show();
               },
               complete: function(){
                   $("#loading").hide();
               },
               success: function(data){
                  $("#feedback").html(data);
               }     
         });
  }else{
        var nom         = $(this).attr('nom');
        var prenom      = $(this).attr('prenom');
        var id_adherent = $(this).attr('rel'); 
        var id_club     = $(this).attr('club');   
        $(this).removeClass('green-stripe');
        $(this).addClass('red-stripe');        
        $(this).html('Valider');
        $.ajax({                    
               url: 'ajax/annuler_demande_couverture.php', 
               data: 'id_adherent=' + id_adherent + '&id_club=' + id_club + '&nom=' + nom + '&prenom=' + prenom, 
               beforeSend: function(){
                   $("#loading").show();
               },
               complete: function(){
                   $("#loading").hide();
               },
               success: function(data){                     
                   $("#feedback").html(data);                      
               }     
     });
    }           
  });
});

Ajax文件#:demande_couverture.php

<?php
    require_once '../includes/config.php';
    $nom          = $_GET['nom'];
    $prenom       = $_GET['prenom'];
    $id_adherent  = intval($_GET['id_adherent']);
    $id_fed       = intval($_GET['id_fed']);
    $id_ligue     = intval($_GET['id_ligue']);
    $id_club      = intval($_GET['id_club']);
    //--------------------------------------------------
    // GET THE CODE-LICENCE FROM `federations` TABLE     //
    //--------------------------------------------------
    $sql = $mysqli->query("SELECT code_licence FROM federations WHERE id_federation=$id_fed");
    $code_licence = $sql->fetch_object();
    $code_licences = explode("-", $code_licence->code_licence);
    $code_licencess = $code_licences[0] ."-". ($code_licences[1]+1);
    $num_licence  = $code_licence->code_licence;
    $num_recu     = substr(number_format(time() * rand(),0,'',''),0,15);
    $validation   = "pending";
    //-------------------------------------------------------------
    // INSERT NEW VALIDATED MEMBER INTO `licences_clubs` table  //
    //------------------------------------------------------------
            $query = $mysqli->query("INSERT INTO `licences_clubs` (id_federation, id_ligue, id_club, id_adherent, num_licence, num_recu, validation) VALUES('$id_fed','$id_ligue','$id_club', '$id_adherent', '$num_licence', '$num_recu', '$validation')") or die($mysqli->error);
    if($query === TRUE){
         echo "     
        <div class='note note-success'>
            <h4 class='block'>Succès !</h4>
            <p>L'athlète <strong style='color:red'>". $nom  ." ". $prenom  ."</strong> was added  to the list of request for coverage.</p>
        </div>
      ";
    }else{
         echo "      
          <div class='note note-danger'>
            <h4 class='block'>Votre attention SVP !!</h4>
             <p> An error occurred while requesting coverage ..</p>
          </div>
      ";
    }
    ?>

代码根本不起作用,找不到bug隐藏在哪里…

我的问题是:如何切换类"绿条"one_answers"红条"…换句话说,如果按钮hasClass为"绿色条纹",我希望它在点击时变为"红色条纹"。类的变化取决于成员信息。

第一条:你的页面中不应该有多个具有相同id的元素

您的php生成多个#validate元素。

$("#validate").on('click', function(e){仅在第一个按钮上绑定功能。

下面是这个问题的演示

<<p> 解决方案/strong>

在您的php中,将id="validate" class="btn default btn-xs red-stripe"替换为class="validateButton btn default btn-xs red-stripe"(和等效的绿色条纹)

在你的javascript中,将$("#validate")替换为$('.validateButton')

最新更新