2016-08-23 4 views
1

Mit Ajax füge ich Tabellenzeilen in ein Dropdown-Menü ein. Es funktioniert und zeigt es an. Ich möchte Ereignisse anhängen, wenn die Zeilen angeklickt werden, aber was ich versucht habe, funktioniert nicht. Was könnte das Problem sein?Ereignis für PHP-Elemente über JQuery anhängen

PHP

<?php 

for($i=1; $i<=2; $i++){ 
    echo "<div class='medium-block dropdown'> 
      <div class='dropdown-toggle stat-dropdown not-added' id='away" . $i . "' data-toggle='dropdown'> 
       <p class='vertical-center add-player' id='add-player" . $i . "' style='margin:0;'>Add Player</p> 
      </div> 
      <ul class='dropdown-menu drop-scroll' style='margin:0; padding:0; border-radius:0;'> 
       <table class='table table-hover' style='margin:0;'> 
        <tbody id='choose-player-away" . $i . "'> 
        </tbody> 
       </table> 
      </ul> 
      </div>"; 
} 
for($i=3; $i<=5; $i++){ 
    echo "<div class='medium-block dropup'> 
      <div class='dropdown-toggle stat-dropdown not-added' id='away" . $i . "' data-toggle='dropdown'> 
       <p class='vertical-center add-player' id='add-player" . $i . "' style='margin:0;'>Add Player</p> 
      </div> 
      <ul class='dropdown-menu drop-scroll' style='margin:0; padding:0; border-radius:0;'> 
       <table class='table table-hover' style='margin:0;'> 
        <tbody id='choose-player-away" . $i . "'> 
        </tbody> 
       </table> 
      </ul> 
      </div>"; 
}?> 

JQuery

<script type="text/javascript"> 
$(document).ready(function(){ 
     scalability(); 
     $(window).resize(scalability); 
     $(".not-added").each(function(i){ 

      $(this).click(function(){ 
       var identifier = $(this).attr('id').charAt(4); 
       var teamid = $(this).attr('id').substring(0,4); 
       if($(this).hasClass("not-added")){ 
        if (window.XMLHttpRequest) { 
         // code for IE7+, Firefox, Chrome, Opera, Safari 
         xmlhttp = new XMLHttpRequest(); 
        } else { 
         // code for IE6, IE5 
         xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");     
         } 

        xmlhttp.onreadystatechange = function() { 
         if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
          document.getElementById("choose-player-"+teamid+identifier).innerHTML = xmlhttp.responseText; 
         } 
        }; 
        xmlhttp.open("GET","getPlayerlist.php?team="+teamid+"&id="+identifier,true); 
        xmlhttp.send(); 

        $(".player").on('click',function(){ 
         alert("working"); 
        }); 
       } 

      }); 

     }); 

    }); 
</script> 

PHP-Datei, die von AJAX geholt wird

<?php 

$team = $_GET["team"]; 
$id = $_GET["id"]; 

$con = mysqli_connect('localhost','root','','sportacus'); 

$query = "SELECT * FROM " . $team . "_team WHERE incourt = 0 ORDER BY number"; 
$result = mysqli_query($con,$query); 

while($row = mysqli_fetch_assoc($result)){ 
    echo "<tr class='player' id='" . $team . "-player" . $row['id'] . "'> 
       <td>" . $row['number'] . "</td> 
       <td>" . $row['first_name'] . " " . $row['last_name'] . "</td> 
      </tr>"; 
} 

mysqli_close($con); 

?> 

Ich möchte die Zeilen mit Klasse .player, die geschaffen werden, durch die AJAX PHP geholt Datei, um anklickbar zu sein.

HINWEIS: Ich verwende Bootstrap-Bibliothek, wenn das hilft. Alles andere funktioniert, mit Ausnahme des Teils:

$(".player").on('click',function(){ 
    alert("working"); 
}); 

Antwort

Verwandte Themen