单击a href删除行(TR)



我有一个表,我想通过单击href标记来删除tr。这是代码。

<tr>                                
    <td data-title="ID">
            echo '<a href="" onclick="return Deleteqry('.$orderID.')";><font size="2" color="#FF0000"><i class=" fa fa-remove" title="Remove this Row"> </i></a>';
    </td>
</tr>
<script>
 function Deleteqry(id)
    { 
      if(confirm("Are you sure you want to delete this Row?")==true)
   $(this).closest('tr').remove(); 
       return false;
    }
</script>

我在SO中检查了其他问题,找到了$(this).closest('tr').remove();,但它不起作用。我没有得到任何错误,但行没有删除。我收到了警告框,但行未删除。

请告诉我我做错了什么。

基本上,正常函数内的this将指向window对象。

.... onclick="return Deleteqry('.$orderID.', this)";>

因此,您必须手动将其传递给内联事件处理程序函数。

function Deleteqry(id,_this) { 
 if(confirm("Are you sure you want to delete this Row?")==true)
   $(_this).closest('tr').remove(); 
   return false;
}

接收它,将其转换为jquery对象并对其进行处理。

最新更新