簡體   English   中英

AJAX創建的項目不響應jQuery選擇器

[英]Items created by AJAX don't respond to jQuery selectors

我有3個頁面: index.php ,帶有update.jsupdate.php

我有一個來自index.php的表。 我使用update.jsupdate.php通過AJAX從數據庫更新表。

問題在於更新后,表中的任何內容均不會響應jQuery選擇器。

這是代碼:

的index.php

<html>
<head>
  <title>Hello!</title>
  <script src="jquery-2.0.3.min.js"></script>
  <script src="update.js"></script>
  <style>
    img#pageloading
    {
        display: none;
        position: fixed;
        padding-top: 350px;
        padding-left: 50%;
    }
  </style>
</head>
<body>
<table border="1">
<thead>
<th>first</th>
<th>second</th>
<th>third</th>
</thead>
<?php
for($i=1;$i<=3;$i++)
{
    ?>
    <tr id="row<?=$i;?>">
    <td>first<?=$i;?></td>
    <td>second<?=$i;?></td>
    <td>third<?=$i;?></td>
    </tr>
    <?php
}
?>
<img src="../submit/loading.gif" id="pageloading" />
</table>
<button id="update">update</button>
</body>
</html>

update.js

$(document).ready(function(){
    $("#update").click(function(){
        $("img#pageloading").css("display", "inline");
        $("#row1").load("update.php", function () {
            $("img#pageloading").css("display", "none");
        });
    });
    $(".update2").click(function(){
        alert("button update2");
    });
});

update.php

<td>first4</td>
<td>second4</td>
<td><button class="update2">third4</button></td>

您正在使用ajax加載元素,因此需要委托事件處理程序

$(document).ready(function(){
    $("#update").click(function(){
        $("img#pageloading").css("display", "inline");
        $("#row1").load("update.php", function () {
            $("img#pageloading").css("display", "none");
        });
    });
    $("#row1").on('click', ".update2", function(){
        alert("buttong update2");
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM