我试图用jQuery $ .post方法从php文件中获取数据。在ajax调用之后,我将附加一个来自php文件的按钮,它将通过删除自身来加载更多帖子,但是在ajax调用之后jQuery不再有效。
var result = function (str,end)
{
$.get("query.php",{ start:str, end: end },function(ajaxresult){
$(ajaxresult).appendTo(".focus");
})
}
result(4,0);
$('#loadmore').on('click', function(e){
result(8,4);
alert('delete');
e.preventDefault();
e.remove();
});
Here is my Php file code.
<?php
$start = $_GET['start'];
$end = $_GET['end'];
$con = mysqli_connect('localhost','root','binarystar','test');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"users");
$sql="SELECT * FROM post ORDER BY id DESC LIMIT $end,$start";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result))
{
echo "<section>";
echo "<span class='feed_username'>" . $row['username'] ."</span> ";
echo "<span class='date'>" . $row['time'] ."</span><br>";
echo "<span class='postx'>" . $row['post'] ."</span><br>";
echo "</section>";
}
echo "<button id='loadmore'>Load More Posts</button>";
mysqli_close($con);
答案 0 :(得分:1)
由于id
是唯一的,因此您需要使用类:
echo "<button class='loadmore'>Load More Posts</button>";
然后,您可以将 event delegation 用于动态加载的元素:
$('body').on('click', '.loadmore', function() {
// Your code here
});