jQueryUI可排序-从可排序列表中删除li
jQueryUI Sortable - Remove a li from the sortable list
我需要的是有一个X,一个文本链接或按钮,我要从可排序列表中删除一个项目。如何在该代码中编写一个函数?
$(function() {
$("#sortable").sortable({
placeholder: "ui-state-musichighlight"
});
});
<ul id="sortable">
<li class="ui-state-musicdefault">
<a class="delete" href="#">X</a>
<!-- ...lots of form data here, only text input fields... -->
</li>
<li>
<a class="delete" href="#">X</a>
<!-- ...lots of form data here, only text input fields... -->
</li>
<li>
<a class="delete" href="#">X</a>
<!-- ...lots of form data here, only text input fields... -->
</li>
</ul>
更新
仍然不起作用,但它在JSfiddle上起作用。。。帮助
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$("#sortable").sortable({
placeholder: "ui-state-musichighlight"
});
$('.delete').click(function(){
$(this).closest('li').remove();
//or
$(this).parent().remove();
});
</script>
<style type="text/css">
#sortable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
#sortable li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px; cursor:move; }
#sortable li span { position: absolute; margin-left: -1.3em; }
#sortable li.fixed{cursor:default; color:#959595; opacity:0.5;}
</style>
<ul id="sortable">
<li class="ui-state-musicdefault">
<a class="delete" href="javascript:void(0)">X1</a>
<!-- ...lots of form data here, only text input fields... -->
</li>
<li>
<a class="delete" href="javascript:void(0)">X2</a>
<!-- ...lots of form data here, only text input fields... -->
</li>
<li>
<a class="delete" href="javascript:void(0)">X3</a>
<!-- ...lots of form data here, only text input fields... -->
</li>
</ul>
更新
我现在已经用…修复了它。。。
//<![CDATA[
window.onload=function(){
然而,它在我的jQuery函数中不起作用。。。帮助
jQuery("#sortable").append('<li class="ui-state-musicdefault"><a class="delete" href="javascript:void(0)">X</a>
$(".delete").click(function(){
$(this).parent().remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li>Delete me 1<a href="#" class="delete">delete</a></li>
<li>Delete me 2<a href="#" class="delete">delete</a></li>
<li>Delete me 3<a href="#" class="delete">delete</a></li>
<li>Delete me 4<a href="#" class="delete">delete</a></li>
</ul>
http://jsfiddle.net/25sp6txz/
监听内部.delete
元素上的委托点击,并在发生这种情况时删除相应的li
:
$("#sortable").sortable({
placeholder: "ui-state-musichighlight"
})
.on('click', '.delete', function() {
$(this).closest('li').remove();
});
演示:http://jsfiddle.net/1htxLzm2/
您可以使用.closest('li')
或.parent()
和单击的删除元素上下文以及.remove()
来删除它们:
$('.delete').click(function(){
$(this).closest('li').remove();
//or
$(this).parent().remove();
});
工作演示
您可以捕获delete
类上的单击,并移除其父类。
(function(){
$(document).on('click', '.delete', function(){
$(this).parent('li').remove();
}
})();
相关文章:
- AngularJS:删除重复的对象并按值对数组进行重新排序
- 在对数组进行排序时删除重复项
- jQueryUI可排序-从可排序列表中删除li
- Magento网站-按选项排序:上下箭头与排序数据绑定,只需删除上下箭头即可
- 无法从jQuery可排序列表中删除新添加的项
- 删除后的剑道网格排序
- JQuery UI:将一个项从非可排序项的可排序项中删除
- CKEDITOR内联在动态创建的元素上(可删除/可排序)
- JSON 合并、数组推送和排序和删除双精度
- 在不删除/附加 DOM 节点的情况下对 DOM 节点进行重新排序
- 如何选择性地禁用可排序的 jQuery UI 的删除
- 如何删除项目的可排序 JQuery
- 删除数组中重复的未排序对象
- Jquery UI 可排序和可删除 - 无法将项目从一个列表克隆到另一个列表
- 删除动态元素并对这些元素的值和 ID 重新排序
- 如何删除空格和重新排序单词
- 如何单击时从排序表标题中删除
- Knockout JS + JQuery UI - 在重新排序后从可观察数组中删除项目的问题
- 如何在删除之前设置 jquery 可排序目标列表的样式
- jquery可排序取消删除基于确认