dongyupen6269 2016-08-23 20:26
浏览 66
已采纳

通过JQuery为PHP创建的元素附加事件

Using ajax I insert table rows in a dropdown menu. It works and displays it. I'd like to attach events for when the rows are clicked, however, what I've tried doesn't work. What could be the problem?

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 file that is fetched by 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);

?>

I want the rows with class .player, which are created by the AJAX fetched php file, to be clickable.

NOTE: I am using bootstrap library if that helps. Everything else works, except for the part:

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

1条回答 默认 最新

  • douanye8442 2016-08-23 20:35
    关注

    Like reported in event binding on dynamically created elements you need to delegate the event.

    In your case, you need to change from:

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

    to:

    $(document).on('click', ".player", function () {
         alert("working");
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画