jquery UI 发送从 ui.item 对象中提取的字符串时出现问题
我有 2 个可排序、相互关联的图片列表:相册和收藏夹。
当我从相册中拖动项目时 ->收藏夹 我想检查它是否已在收藏夹列表中。
我有一个功能可以检查图片是否在收藏夹列表中:
function isInFavorites(url) {
return $(".favorites li img[src*='" + url + "']").length > 0;
}
此功能按预期工作...
但是,当我使用 ui.item 提取 scr attr 并将参数传递给该函数时,我总是得到一个真正的布尔值?
var itemSrc = ui.item.find("img").attr("src");
if (isInFavorites(itemSrc)) { alert('item allready in favorites, do not accept'); }
else { alert('OK, now clone back to album'); }
我一直在努力解决这个问题,希望得到一些帮助!
JS Fiddle 可以在这里找到: http://jsfiddle.net/tunafish/CTps3/
干杯!
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 not 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定这是否是处理逻辑的最佳方法,但事件触发的顺序是问题的根源
此事件在项目移动后运行。如果它是重复项,则长度将为 2,但长度始终为 1,因为您刚刚将该项目移动到下方列表中。
快速解决方法是测试
$(".favorites li img[src*='" + url + "']").length > 1
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
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