2012-01-06 27 views
0

我已經完成了在jQuery中拖放元素的框架。現在我想根據需要從框架中刪除元素。刪除應該從鍵盤上刪除。我正在嘗試並可以選擇該項目,但不能刪除所選項目。 我的代碼,這樣的..從jQuery框架中刪除從鍵盤板塊中選擇的項目

 //Select the element 
     jQuery(function() { 
      jQuery ("#frame").selectable(); 
     }); 
     //move to trash_icon 
     jQuery 
HTML code... 
    <div id="frame"> 
    <span id="title"></span> 
    <div id="tbldevs" > </div> 
    </div><!-- end of frame --> 

我想知道的是有jQuery中的任何功能刪除可以從鍵盤發生。

回答

2

您需要簡單地處理鍵盤事件,就像這樣:

HTML:

<div id="frame"> 
    <span id="title">My Title</span> 
    <div id="tbldevs"> 
     <div id="item1"> This is item 1</div> 
     <div id="item2"> This is item 2</div> 
    </div> 
    <input type="button" id="sel-cancel" value="Cancel Select"/> 
</div> 

JS:

jQuery(function() { 
    //make the elements selectable 
    $("#tbldevs").selectable(); 

    //handle the events in every element. Only applies to elements which can be handle focus 
    $('*').keypress(function(event) { 
     if (event.which == 100) { //this is the keycode. 100 is the 'd' key. The delete key is difficult to bind. 
      $('.ui-selected').remove(); 
      //you can add ajax calls here to remove items from the backend 
     } 
    }); 
}); 

注意:下面的代碼只會從HTML樹的元素。

相關問題