简体   繁体   中英

Remove parent element with class

i have the following html structure ...

  <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>       

Now i am binding a click event to the button with class extract_bt ...

on the click event i am posting some data to the server if the responce is true then i need to remove the element TR with class table_opening_CL and its inner HTML too.

i alerted the value of $(this).html(); inside the .post ,but it returns NULL .

does we need to store this before posting ?

please help me

Thank you.

update :

this is the code i used ...

 $(".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");
});    
});

Yes, inside the post success handler this will not point to the DOM element so $(this).html() will not work.

I think you are looking for something like this.

$('.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();
         }
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM