如何计算删除时的元素,这样我最终得不到空()? [英] How to count the elements on deletion so I not end up with an empty ()?
问题描述
我有以下代码:
<ul class="ul" id="selected_conditions">
<li data-field="asset_locations_name" data-condition="in">
<i class="fa fa-minus-circle delete_condition" aria-hidden="true" title="Click to remove this condition from the list"></i> WHERE asset_locations_name IN(
<span class="condition_item" data-id="1213381233">
<i class="fa fa-minus-circle delete" title="Click to remove this item from the list" aria-hidden="true"></i> 1213381233
</span>,
<span class="condition_item" data-id="1212371897">
<i class="fa fa-minus-circle delete" title="Click to remove this item from the list" aria-hidden="true"></i> 1212371897
</span> )
</li>
</ul>
每次点击小图标 .delete
我应该删除当前值,我可以使用以下代码实现:
Each time I click on the little icon .delete
I should remove the current value and I was able to achieve that with the following code:
$(function() {
$('#selected_conditions').on('click', '.delete', function(ev) {
var item_id = $(this).parent().data('id');
$('.condition_item[data-id="'+ item_id +'"]').remove();
});
});
但上面的代码有问题:如果我删除所有项目,我将以下列结果()
空字符串,我不能这样:
But the code above has a problems: if I remove all the items I will end up with the following ()
empty string and I can't have it so:
- 怎么做我在括号
()
中计算SPAN,如果点击最后一个,我会发一个警告?
- How do I count the SPAN inside the parenthesis
()
and trow an alert if I am clicking on the last one?
注意:正确的行为是向用户显示一个确认对话框
和if用户单击确定并同意删除最后一项,然后应删除
整个条件。这意味着如果我点击最后一个元素>应该删除整个父li
。
有任何帮助吗?我已经给你留下了小提琴。这是一个WIP,所以如果你有一个建议或更好的解决方案随时可以添加到你的答案。
Any help? I have leave you a Fiddle to play with. This is a WIP so if you have a suggestion or better solution feel free to add it to your answer.
推荐答案
我是升级你的jsfiddle例子,请检查它
I've upgrading your jsfiddle example, please check it
$(function() {
$('#selected_conditions').on('click', '.delete', function(ev) {
var item = $(this).closest(".condition_item");
var condition = $(this).closest(".condition");
var items = condition.find(".condition_item");
if(items.size() > 1) {
item.remove();
} else {
if(confirm("Removing last item will remove whole condition. Continue?")) {
condition.remove();
}
}
});
});
https://jsfiddle.net/br3t/8184ok2e/8/
这篇关于如何计算删除时的元素,这样我最终得不到空()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!