English 中文(简体)
jQuery, remove item from middle of list, then refresh list?
原标题:

I have a ul list that is loaded by an included PHP file. Those items have a "Delete" link that removes them from the list and the database.

When an item is removed from the center of the list using this code:

$list.find( #city_  + $id).remove();

It leaves a space in the list like this:

alt text
(source: jamespwright.com)

What can I do to refresh that list, without going back to the database and reloading the entire thing?

EDIT

Here is the example code:

<ul id="city_list">
    <li id= city_7 >Eureka - <a href= city.modify.php?id=7&modification_type=edit >Edit</a> - <a href= #  delete_id= 7  class= confirm_delete >Delete</a></li> <br />
    <li id= city_8 >Rolla - <a href= city.modify.php?id=8&modification_type=edit >Edit</a> - <a href= #  delete_id= 8  class= confirm_delete >Delete</a></li> <br />

    <li id= city_10 >Union - <a href= city.modify.php?id=10&modification_type=edit >Edit</a> - <a href= #  delete_id= 10  class= confirm_delete >Delete</a></li> <br />

最佳回答

I m guessing your HTML looks like this:

<li><div id="city01">Eureka - <a...>Edit</a> - <a...>Delete</a></div></li>

In this case you would need to remove the parent of the selector

$list.find( #city_  + $id).parent().remove();
问题回答

id="city_X" is duplicated on the <li> and the <label>. An id must be unique in the document or you will have undefined behavior when trying to do $( #my_id ).

<label>s are for form input elements, not for text display. Either use no tag or <span>

Here is how I would do it:

<ul id="city_list">
    <li id= city_7  class="city">
        <span class="label">Eureka</span>
         - <a href= city.modify.php?id=7&modification_type=edit >Edit</a>
         - <a href= #  class= confirm_delete >Delete</a>
    </li> 
    <li id= city_8  class="city">
        <span class="label">Rolla</span>
         - <a href= city.modify.php?id=8&modification_type=edit >Edit</a>
         - <a href= #  class= confirm_delete >Delete</a>
    </li>        
    <li id= city_10  class="city">
        <span class="label">Union</span>
         - <a href= city.modify.php?id=10&modification_type=edit >Edit</a>
         - <a href= #  class= confirm_delete >Delete</a>
    </li> 
</ul>

Assuming you want to delete when clicking on the delete link.

// Use event delegation
$( #city_list ).bind( click , function(event)
{
    $(event.target).closest( .confirm_delete ).each(function()
    {
        // Get ID from city
        var $city = $(this).closest( .city );
        var id = $city.attr( id ).match(/city_(.+)/)[1];

        // Do any AJAX request to tell the server to delete the city

        $city.remove();
    })
})

$id holds a simple value, not a wrapped set? .. well, ...ok...

do it like this

$( #city_  + $id).parent().remove();

or

$( div:has(#city_  + $id +  ) ).remove();

you don t need to use $list or $().find() if your html is properly formed w/ unique ids

You could try this

$list.find( #city_  + $id).hide( fast , function() {
     $(this).remove();
}); 

You could use FireBug to see what you re actually removing, but I m guessing it s the label element, not the li element. The way you styled it makes it look as if it s an empty space, while I think it s an empty list element.

How about this:

$list.find( li#city_  + $id).remove();
$list.find( #city_  + $id).next().remove() /* remove the <br/> */
     .end() /* go back to what was found originally */
     .remove(); /* and remove */




相关问题
Finding a class within list

I have a class (Node) which has a property of SubNodes which is a List of the Node class I have a list of Nodes (of which each Node may or may not have a list of SubNodes within itself) I need to be ...

How to flatten a List of different types in Scala?

I have 4 elements:List[List[Object]] (Objects are different in each element) that I want to zip so that I can have a List[List[obj1],List[obj2],List[obj3],List[obj4]] I tried to zip them and I ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

Is List<> better than DataSet for UI Layer in ASP.Net?

I want to get data from my data access layer into my business layer, then prepare it for use in my UI. So i wonder: is it better to read my data by DataReader and use it to fill a List<BLClasses&...

What is the benefit to using List<T> over IEnumerable<T>?

or the other way around? I use generic lists all the time. But I hear occasionally about IEnumerables, too, and I honestly have no clue (today) what they are for and why I should use them. So, at ...

灵活性:在滚动之前显示错误的清单

我有一份清单,在你滚动之前没有显示任何物品,然后这些物品就显示。 是否有任何人知道如何解决这一问题? 我尝试了叫人名单。

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签