Javascript 当有';使用jquery的列表中没有其他项
在我的应用程序中,用户单击删除按钮从列表中删除项目。当最后一个项目被删除时,它应该显示一条消息,说明没有更多的项目。我的代码可以很好地删除项目,但在最后一个项目之后,它不会显示消息。为什么呢 Jquery:Javascript 当有';使用jquery的列表中没有其他项,javascript,jquery,html,Javascript,Jquery,Html,在我的应用程序中,用户单击删除按钮从列表中删除项目。当最后一个项目被删除时,它应该显示一条消息,说明没有更多的项目。我的代码可以很好地删除项目,但在最后一个项目之后,它不会显示消息。为什么呢 Jquery: var optionLinkBox = $('.option-lnk'); optionLinkBox.delegate('.um-delete-lnk', 'click', function(e){ var thisElem = $(this); thisElem.cl
var optionLinkBox = $('.option-lnk');
optionLinkBox.delegate('.um-delete-lnk', 'click', function(e){
var thisElem = $(this);
thisElem.closest('li').fadeTo(400, 0, function(){
$(this).slideUp(400, function(){
$(this).remove();
});
if($('.um-card-detail li').length < 1){
// Message to show after the last item
$('section:first').text('You don\'t have any payment card saved.');
}
});
e.preventDefault();
});
var optionLinkBox=$('.option-lnk');
OptionLink Box.delegate('.um删除lnk','click',函数(e){
var thisElem=$(本);
thisElem.closest('li').fadeTo(400,0,function(){
$(this).slideUp(400,function(){
$(this.remove();
});
if($('.um卡详细信息li')。长度<1){
//要在最后一项之后显示的消息
$('section:first').text('您没有保存任何支付卡');
}
});
e、 预防默认值();
});
HTML:
-
您的卡(这是您的默认卡)
|
一些付款细节
-
你的名片
|
一些付款细节
我认为在删除最后一个li元素之前就已经执行了检查(因为该元素在fadeIn的回调中被删除)
将检查最后一个li的代码移动到fadeIn方法的回调中,它就会工作
试试这个:
var optionLinkBox = $('.option-lnk');
optionLinkBox.delegate('.um-delete-lnk', 'click', function(e){
var thisElem = $(this);
thisElem.closest('li').fadeTo(400, 0, function(){
$(this).slideUp(400, function(){
$(this).remove();
if($('.um-card-detail li').length < 1){
// Message to show after the last item
$('section:first').text('You don\'t have any payment card saved.');
}
});
});
e.preventDefault();
});
var optionLinkBox=$('.option-lnk');
OptionLink Box.delegate('.um删除lnk','click',函数(e){
var thisElem=$(本);
thisElem.closest('li').fadeTo(400,0,function(){
$(this).slideUp(400,function(){
$(this.remove();
if($('.um卡详细信息li')。长度<1){
//要在最后一项之后显示的消息
$('section:first').text('您没有保存任何支付卡');
}
});
});
e、 预防默认值();
});
这是因为:
if($('.um-card-detail li').length < 1){
// Message to show after the last item
$('section:first').text('You don\'t have any payment card saved.');
}
提供给slideUp方法的函数回调将在slide操作完成后执行,并且在当前函数返回400毫秒后,您应该将if语句放入回调中
$(this).slideUp(400, function(){
$(this).remove();
if($('.um-card-detail li').length < 1){
// Message to show after the last item
$('section:first').text('You don\'t have any payment card saved.');
}
});
$(this).slideUp(400,function(){
$(this.remove();
if($('.um卡详细信息li')。长度<1){
//要在最后一项之后显示的消息
$('section:first').text('您没有保存任何支付卡');
}
});
原因是您有$(this).remove();包装在slideUp()函数中,执行该函数需要400毫秒。IF语句不会等待slideUp完成执行。要实现这一点,请将语句放在slideUp回调中,如下所示:
$(this).slideUp(400, function(){
$(this).remove();
}, function() {
if($('.um-card-detail li').length < 1){
// Message to show after the last item
$('section:first').text('You don\'t have any payment card saved.');
}
});
$(this).slideUp(400,function(){
$(this.remove();
},函数(){
if($('.um卡详细信息li')。长度<1){
//要在最后一项之后显示的消息
$('section:first').text('您没有保存任何支付卡');
}
});
@Cybermate:非常感谢您的支持。它起作用了。我会在10分钟后接受你的回答,所以给出的答案已经过了。再次感谢。
$(this).slideUp(400, function(){
$(this).remove();
if($('.um-card-detail li').length < 1){
// Message to show after the last item
$('section:first').text('You don\'t have any payment card saved.');
}
});
$(this).slideUp(400, function(){
$(this).remove();
}, function() {
if($('.um-card-detail li').length < 1){
// Message to show after the last item
$('section:first').text('You don\'t have any payment card saved.');
}
});