用class删除父元素

时间:2012-01-28 05:52:12

标签: javascript jquery html

我有以下html结构...

  <tr class="table_opening_CL">
    <td>
  <button class="extract_bt">Approve</button>
  <button class="extract_bt">Delete</button><br>
  <input name="featured" class="featured_selector" type="checkbox" />
  Featured<input name="public_selector" class="public_selector" type="checkbox" />
  Public
   </td> 
        <td>25</td> 

        <td>Session</td> 

        <td>Beek</td> 

        <td>dPC7t</td> 
        <td>2012-01-27 23:38:19</td> 
        <td>Abstract</td> 

        </tr>       

现在,我将点击事件绑定到button,其中包含extract_bt ...

在click事件上我将一些数据发布到服务器如果response为true,那么我需要删除带有TR类及其内部HTML的元素table_opening_CL

我在$(this).html();内警告.post的值,但它返回NULL

我们是否需要在发布之前存储this

请帮帮我

谢谢。

更新:

这是我用过的代码......

 $(".extract_bt").live('click',function(){
var p_key = $(this).parent().next().next().next().next().text();
var p_id = $(this).parent().next().text();
var fchkd = $(this).parent().find(".featured_selector").prop("checked");
var pchkd = $(this).parent().find(".public_selector").prop("checked");

$.post("url",{PACKAGE_KEY:p_id,FEATURED:fchkd,PUBLIC:pchkd,PACKAGE:p_key},function(data){
  if (data)
  {
  alert($(this).html());
     $(this).parents(".table_opening_CL").remove();
  }
  else 
  {
    alert(data);
  }

  },"json");
});    
});

1 个答案:

答案 0 :(得分:4)

是的,post成功处理程序this内部不会指向DOM元素,因此$(this).html()将无效。

我认为你正在寻找类似的东西。

$('.extract_bt').click(function(){
   var $btn = $(this);
    $.post("urlOfThePage", {}, function(response){
         //If reponse will be boolean you can just say if(response)
         if(response == true){
            //This will remove the whole tr element including child elements.
            $btn.closest('tr.table_opening_CL').remove();
         }
    }
});
相关问题