开发者

jQuery + <a> tag

开发者 https://www.devze.com 2022-12-17 21:15 出处:网络
I\'ve created a Facebook/Twitter style status update where the new Status\' get added to an unordered list. I now want to add a \"REMOVE\" function to it, however, I\'m not sure how to best accomplish

I've created a Facebook/Twitter style status update where the new Status' get added to an unordered list. I now want to add a "REMOVE" function to it, however, I'm not sure how to best accomplish this.

  1. In my list item, create my [a] tag with the unique ID of the status post ID and set a listener class
  2. Have jQue开发者_开发知识库ry listen for [a] tags with the listener class
  3. POST the ID over to the PHP script to remove the post; if successful return with "ok" or if failed return with "fail"
  4. In the callback function, if it's OK, then find the element and remove it


HTML:

<li id="id-1234">
   Some text
   <a href="#" class="remove_li">Remove</a>
</li>

Javascript:

$(document).ready(){
    $('.remove_li').live('click', function(){
       var id = $(this).parent().attr('id').substring(3);
       var that = this;
       $.post('url.php', {id: id}, function(data, status){
           if(status == 'success'){
               $(that).parent().remove();
           } else {
               alert("Couldn't delete");
           }
       });
    });
}
0

精彩评论

暂无评论...
验证码 换一张
取 消