javascript
  • php
  • jquery
  • ajax
  • twitter-bootstrap
  • 2016-08-23 3 views 1 likes 
    1

    ajaxの使用ドロップダウンメニューに表の行を挿入します。それは動作し、それを表示します。行をクリックしたときにイベントをアタッチしたいのですが、私が試したことは機能しません。何が問題なの?JQuery経由でPHPで作成された要素のイベントを添付します。

    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> 
    

    AJAX

    <?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); 
    
    ?> 
    

    に取り込まれるPHPファイル私はAJAXフェッチPHPで作成されたクラス.player、との行をしたいですファイル、クリック可能にする。

    注:私はブートストラップライブラリを使用しています。一部を除いて他のすべて作品、:

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

    答えて

    0

    event binding on dynamically created elementsで報告されたと同じようにあなたがイベントを委任する必要があります。

    $(".player").on('click',function(){ 
    

    $(document).on('click', ".player", function() { 
        alert("working"); 
    }); 
    
    あなたの場合

    、あなたがから変更する必要があります

    関連する問題