2013-05-15 23 views
0

我做了一個快速列表,其中每個項目可以移動到垃圾桶。我一旦將它追蹤到垃圾上,它就會隱藏起來。如果您點擊「確定」,則使用remove()將其完全刪除。ui.draggable顯示是否取消確認。

問題是,如果我點擊取消確認,它會保持隱藏狀態(但我希望它再次顯示,因爲它會返回到原來的位置,而不是隱藏)。我嘗試過在許多地方使用ui.draggable.show(),但沒有運氣。有什麼建議?

這裏是的jsfiddle:http://jsfiddle.net/Gdze8/6/

的Javascript/jQuery的:

$(init) 

function init() { 
$(".contentItem").draggable({ 
    revert: function (event, ui) { 
     if ($(event[0]).closest('.list4').length) { 
     var state = !confirm("Are you sure you want to delete?") 
     if (!state) { 

     $(this).remove(); 
     bottomInfo(); 
    } else { 
     return state; 
    } 
    } else { 
     return true; 
    } 
    } 
}); 
$(".list4").droppable({ 
    accept: ".contentItem", 

    drop: function(event, ui) { 
    ui.draggable.hide(); 


    return true; 
    } 
}); 
} 

回答

2

一,你可以做到這一點的方法是簡單地設置拖動到隨時恢復,然後允許可放開函數來處理是否它應該被刪除或不。這裏是你的小提琴的JS,我明白你的問題的作品:

jQuery(function() { 
    jQuery('.contentItem').draggable({ 
     revert: true 
    }); 
    jQuery('.list4').droppable({ 
     accept: '.contentItem', 
     drop: function (event, ui) { 
      ui.draggable.hide(); 
      if (confirm('Are you sure you want to delete?')) { 
       ui.draggable.remove(); 
      } else { 
       ui.draggable.show(); 
      } 
     } 
    }); 
}); 

這裏是你更新的提琴:http://jsfiddle.net/Gdze8/11/

編輯:不知道什麼bottomInfo()是,但你完全可以直接調用它在ui.draggable.remove()下方;如果需要。

+0

的事情是我希望它在第一隱藏,當你將其拖到框。隨着你做它的方式,一旦它在框中,它仍然顯示(在彈出過程中)。 –

+0

更新後隱藏,直到做出選擇。 – Duffmaster33

0

嘗試把所有的確認功能在drop事件上可放開:

$(".contentItem").draggable({ 
    revert: true 
}); 
$(".list4").droppable({ 
    accept: ".contentItem", 
    drop: function(event, ui) { 
     var state = !confirm("Are you sure you want to delete?") 
     if (!state) { 
      $(ui.draggable).remove(); 
      bottomInfo(); 
      } else { 
       return state; 
      } 
     return true; 
    } 
}); 

http://jsfiddle.net/UGY2v/

+0

這仍然不能回答問題。我不認爲我的問題很清楚。我想讓它在您拖入時隱藏,但如果您按取消確認,它會回滾並顯示。 –

0

編輯經進一步調查,你不應該需要滴功能都沒有。

jsFiddle

$(init); 

function init() { 
    $(".contentItem").draggable({ 
     revert: function (event, ui) { 
      if ($(event[0]).closest('.list4').length) { 
       var state = !confirm("Are you sure you want to delete?"); 
       if (!state) { 

        $(this).remove(); 
        bottomInfo(); 
       } else { 
        return state; 
       } 
      } else { 
       return true; 
      } 
     } 
    }); 
    $(".list4").droppable({ 
     accept: ".contentItem" 
    }); 
} 
+0

我想保留.hide(),因爲我希望它在隱藏垃圾時隱藏。我只想讓它顯示在彈出「確認」時是否按下取消。 –