duanmu3049 2015-05-26 14:51
浏览 69
已采纳

在Ajax成功后删除表行

I have a table that will display images stored in a DB as well as some other information. I have a column that has a link that the user can click to delete the image, this works fine however when the user does this I also want to remove the row that the user selects to remove but I can't seem to get it to work.

function removeSectionPhoto(image, section_id) {

    if(confirm("Are you sure you want to delete this image?") == true) {

        $.ajax({
            type: "POST",
            url:  "remove_section_photo.php",
            data: 'image='+image+'&sectionID='+section_id,

            success: function(data){
                //this is supposed to remove the row but currently doesnt work
                $("#"+$(this).data("remove-row")).remove(); 
            }
        }); 
    }
}

The table rows are being output from PHP, this is what one of the table rows looks like:

echo "<tr id='fac_sec_photo_row". $imageRowCount ."'>";
echo "<td><a><img class='section_photo' src='" . $sectionPhotoRow['photo'] . "' alt='Section Photos' height='50' width='50'</a></td>";
echo "<td><input type='text' id='photoDesc' name='photoDesc[]' value='" . $sectionPhotoRow['photo_desc'] . "'</td>";
echo "<td><input type='file' id='new_section_photo' name='new_section_photo[]' onchange='displaySecImage(this)'></td>";
echo "<td><a href='#' class='removeImageRow' data-remove-row='fac_sec_photo_row". $imageRowCount . "' onClick='removeSectionPhoto(\"". $sectionPhotoRow['photo'] ."\", \"". $facilitySecId ."\")'>Remove</a></td>";
echo "</tr>";

What am I doing wrong, how can I remove the selected table row?

  • 写回答

2条回答 默认 最新

  • doufang7385 2015-05-26 15:22
    关注

    All right. I see part of the issue. You are mixing vanilla javascript with jquery. Jquery does a LOT behind the scenes that hides what javascript doesn't do. Since you are using a vanilla onclick inline on the element, this will likely refer to window, not the element that is being clicked on (I know, confusing). Jquery uses the function.prototype.apply method to re-map this to the target element. Javascript doesn't. But all is not lost, and there is an easy fix. The easiest of which is to just pass in $imageRowCount to your function as well. Then you can simply reference the id directly.

    function removeSectionPhoto(image, section_id, row_id) {
    
        if(confirm("Are you sure you want to delete this image?") == true) {
    
            $.ajax({
                type: "POST",
                url:  "remove_section_photo.php",
                data: 'image='+image+'&sectionID='+section_id,
    
                success: function(data){
                    //this is supposed to remove the row but currently doesnt work
                    $("#"+row_id).remove(); 
                }
            }); 
        }
    }
    

    And then:

    echo "<td><a href='#' class='removeImageRow' onClick='removeSectionPhoto(\"{$sectionPhotoRow['photo']}\", \"{$facilitySecId}\", \"fac_sec_photo_row{$imageRowCount}\")'>Remove</a></td>";
    

    Another option would be to pass in this to the function call in the onclick='removeSectionPhoto(this, ...etc...)' which would give your first argument (or whatever argument number you pass it in) a reference to that variable. Then $(ele).closest('tr').remove() would work.

    Really though, you shouldn't mix vanilla js with jquery. Just use jquery to query for the elements and use it's event handlers and this wouldn't have been an issue (well, you still need to map this to self).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度