dow66098 2016-02-20 14:14
浏览 46
已采纳

使用AJAX将操作附加到最后一个附加行

I am using AJAX to send data to server and update the current page with no reloading. I have this script:

$.ajax
        ({
            url: 'insert.php', 
            type: 'POST', 
            data: {data1: emp, data2: pos, data3: sal},
            dataType: "json", 

            success:function(data)
            {
                    var emp_n = data.emp_name;
                    var btn = '<button type="Button" id="del" name="del" class="btn btn-danger">Delete</button></a>';
                    $("#before_tr").before("<tr><td>"+data.emp_name+"</td><td>"+data.position+"</td><td>"+data.salary+"</td><td>"+btn+"</td></tr>");

            },

As you see, I have a delete button that should be added too to the same row. But this button won't be active until I refresh the page. What I want is to append action like this PHP Based code for a delete button of each row:

<tr id="<?php echo $row['id']; ?>">
                    <td contenteditable><?php echo $row['emp_name'] ?></td>
                    <td contenteditable><?php echo $row['position'] ?></td>
                    <td contenteditable><?php echo $row['salary'] ?></td>
                    <td><button type="Button" id="del" name="del" class="btn btn-danger">Delete</button>
                </tr>
                <?php } ?>

What I've tried is the following:

$.ajax
        ({
            url: 'insert_with_ajax.php', //Sending variable emp, pos, and sal, into this url
            type: 'POST', //I will get variable and use them inside my PHP code using $_POST['emp']
            data: {data1: emp, data2: pos, data3: sal},//Now we can use $_POST[data1];
            dataType: "json", //JSON or HTML

            success:function(arr)
            {
                //if(data=="success")
                //{
                    //alert("Data added");
                    var emp_n = arr.emp_name;
                    var btn = '<a href="delete_id.php?id="'+emp_n+'><button type="Button" id="del" name="del" class="btn btn-danger">Delete</button></a>';
                    $("#before_tr").before("<tr><td>"+arr.emp_name+"</td><td>"+arr.position+"</td><td>"+arr.salary+"</td><td>"+btn+"</td></tr>");
                    $("#emp_name").val("");
                    $("#position").val("");
                    $("#salary").val("");
                //}
            },

Where I added this line <a href="delete_id.php?id="'+emp_n+' to the following:

var btn = '<a href="delete_id.php?id="'+emp_n+'><button type="Button" id="del" name="del" class="btn btn-danger">Delete</button></a>';

When I click on delete button of the last one added using AJAX the page go to delete_id but the link is like this:

delete_id.php?id=

id is equal to empty.

So, What I am working on is like when we add a status on Facebook and you delete it directly with no need for reloading the page. I am trying and I hope that someone could help.

  • 写回答

1条回答 默认 最新

  • dongyingjiu0669 2016-02-21 10:46
    关注

    Check your code once again:

    '<a href="delete_id.php?id="'+emp_n+'>' 
                               ^ - see? you have a closing " here.
    

    This means that no values will be added to your href attribute as it's already closed.

    Proper code is:

    '<a href="delete_id.php?id='+emp_n+'">'
                                        ^ -see? closing " moved after emp_n
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 被蓝屏搞吐了,有偿求帮解答,Ai回复直接拉黑
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并