weixin_33717117 2015-11-12 12:23 采纳率: 0%
浏览 21

ajax中的ajax无法正常工作

I'm loading php output into a div using ajax on a page(jobs.php), this output is refreshed every few seconds to show an up to date list of records from sql.

I am trying to use ajax to delete a chosen record, this works fine on a stand alone page however when I try and do it from the jobs.php page nothing happens.

$(function () {
    $('.trash').click(function () {
        var del_id = $(this).attr('id');
        var $ele = $(this).parent().parent();

        $.ajax({
            type: 'POST',
            url: 'deleterecord.php',
            data: {del_id: del_id},
            success: function (data) {
                if (data == "YES") {
                    $ele.fadeOut().remove();
                } else {
                    alert("record could not be deleted")
                }
            }
        });
    });
});

deleterecord.php

$delid = $_POST['del_id'];

$query5 = "DELETE from Records WHERE id = '$delid'";
$sql5 = mysqli_query($connection, $query5) or die(mysqli_connect_error());

if(isset($sql5)) {
    echo "YES";
} else {
    echo "NO";
}

jobs.php

<script type="text/javascript">
    refreshdiv();
</script>

any suggestions on where I could be going wrong??

function refreshdiv() {

    // The XMLHttpRequest object

    var xmlHttp;
    try {
        xmlHttp = new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
    } catch (e) {
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
        } catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                alert("Your browser does not support AJAX.");
                return false;
            }
        }
    }
}
  • 写回答

1条回答 默认 最新

  • weixin_33696822 2015-11-12 12:35
    关注

    Since the delete buttons are loaded dynamically you need to use delegated events.
    So we need to bind the click event to a parent element that exists at load time,
    ex body.

    Replace this

    $('.trash').click(function () {
    

    With this

    $('body').on('click','.trash',function(){
    
    评论

报告相同问题?

悬赏问题

  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来