I have 2 sortable, connected lists with pics: Album and Favorites.
When I drag and item from Album -> Favorites I want to check if it's already in the Favorites list.I have a function that checks if the pics are in the Favorites list:
function isInFavorites(url) {
return $(".favorites li img[src*='" + url + "']").length > 0;
}
This function works like expected...
However when I extract the scr attr with ui.item and pass the argument to this function I always get a true boolean??var itemSrc = ui.item.find("img").attr("src");
if (isInFavorites(itemSrc)) { alert('item allready in favorites, do no开发者_开发技巧t accept'); }
else { alert('OK, now clone back to album'); }
I have been banging my head way to long on this and would appreciate some help!
A JS Fiddle can be found here: http://jsfiddle.net/tunafish/CTps3/Cheers!
Not sure if this is the best way to process the logic but the order the events are firing is the source of your problem
function isInFavorites(url) {
return $(".favorites li img[src*='" + url + "']").length > 0;
}
This event runs AFTER the item has been moved. if it is a duplicate you will have length 2, but you will always have length 1 because you just moved the item into the lower list.
quick fix is to test for $(".favorites li img[src*='" + url + "']").length > 1
精彩评论