我有一种情况,我需要使用 JQuery Nestable 库,可以编辑或删除列表中的项目。
我试图为此开发一个解决方案,但它没有按预期编辑或删除项目:
HTML:
<section class="panel panel-margin-settings" id="first-component">
<header class="panel-heading">
<h2 class="panel-title">Categories</h2>
</header>
<a class="new-category" id="add-new-app-category">+ New Category</a>
<div class="panel-body">
<div class="dd" id="nestable">
<ol class="dd-list" id="dd-list-app-categories-container">
<li class="dd-item" data-id="1">
<div class="dd-handle">
<span id="category-item">Item 1</span>
<a href="#" class="close close-assoc-file"
data-dismiss="alert" aria-label="close">×</a>
</div>
</li>
</ol>
</div>
</div>
</section>
JS:
// activate Nestable for list 1
$('#nestable').nestable()
// FOR ADDING
var nestablecount = 1;
$('#add-new-app-category').click(function () {
$('#dd-list-app-categories-container').append('<li class="dd-item" data-id="' + (++nestablecount) + '"><div class="dd-handle"><span id="category-item">Item ' + nestablecount + '</span>' +'<a href="#" class="close close-assoc-file" data-dismiss="alert" aria-label="close">×</a>' + '</div>' + '</li>');
});
// FOR REMOVING
$("#dd-list-app-categories-container").on("click", ".close", function (event) {
$(this).closest('li').remove();
if (nestablecount > 0)
nestablecount--;
});
// FOR EDIT
$('#dd-list-app-categories-container').on('dblclick', '#category-item', function () {
var $input = $('<input type="text" value="' + $(this).text().trim() + '" />');
$(this).replaceWith($input);
$input.select();
}).on('blur', 'input', function () {
$(this).replaceWith('<span>' + $(this).val() + '</span>');
});
我在 JSFiddle 中做了一个例子: https ://jsfiddle.net/6b3m348q/6/
这个问题的解决方案是什么?