从jQuery框架中删除从键盘板块中选择的项目
问题描述:
我已经完成了在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中的任何功能删除可以从键盘发生。
答
您需要简单地处理键盘事件,就像这样:
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树的元素。