初始运行后无法执行jquery post或ajax [英] can't execute jquery post or ajax after initial run

查看:66
本文介绍了初始运行后无法执行jquery post或ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首次成功点击后,我似乎无法使下面的代码正常工作.我尝试$ .post和ajax都无济于事.删除所有项目的初始点击均有效,但随后的点击均无效

I can't seem to make the code below work after an initial successful click. I tried both $.post and ajax to no avail. The initial click to delete any of the items work, but the succeeding clicks do not work

<script>    
$(document).ready(function(){
$('[id^="del"]').click(function(){
    var valname = $(this).attr('id').split('del_');
    /*$.post("delthis.php", {id: ""+valname[1]+""}, function(data) {
            $("#dynamic_section").html(data);
    });*/
    $("#dynamic_section").load('delthis.php', {"id":valname[1]} );
    return false;
  });
});
</script>

<body>

<div id='static_section'>This is the static section</div>
<div id='dynamic_section' style='border: 1px solid black;'>
<?php
// db connection here
$sql = mysql_query("SELECT * from test_table ORDER BY id");
while ($row = mysql_fetch_array($sql))
{
?>
<p><a href='#' id='del_<?php echo $row['id']; ?>'>Hello <?php echo $row['id']; ?></a></p>
<?php
} 
?>
</div>

这是简单的delthis.php文件:

This is the simple delthis.php file:

<?php
// db connection 
//$id = $_POST['id'];
$id = $_REQUEST['id'];

$sql = mysql_query("DELETE from test_table WHERE id = '$id'");

$sql = mysql_query("SELECT * from test_table ORDER BY id");
while ($row = mysql_fetch_array($sql))
   $html .= '<p><a href="#" id="del_' . $row['id'] . '">Hello ' . $row['id'] . '</a></p>';

echo $html;

?>

感谢所有帮助/指针. TIA!

Appreciate any help/pointers. TIA!

推荐答案

问题是您替换了#dynamic_section的所有内容,因此必须重新绑定" click事件.试试这个.

The problem is that you replace all the content of the #dynamic_section so you have to "re-bind" the click event. Try this.

<script>    
$(document).ready(function(){
    function doPost(evt) {
        evt.preventDefault();
        var valname = $(this).attr('id').split('del_');
        $.post("delthis.php", {id: ""+valname[1]+""}, function(data) {
            $("#dynamic_section").html(data);
            $('[id^="del"]').click(doPost);
        });
    }

    $('[id^="del"]').click(doPost);
});
</script>

祝你好运!

这篇关于初始运行后无法执行jquery post或ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆