2013-08-19 44 views
-1

請參考下面的代碼中發表評論:如何在jQuery的「.EACH」函數中執行「CONTINUE」語句?

$('#btnDelete').on('click', function() 
{ 
    var treeView = $("#treeview").data("kendoTreeView"); 
    var userId = $('#user_id').val(); 

    $('#treeview').find('input:checkbox:checked').each(function() 
    { 
     debugger; 

     var li = $(this).closest(".k-item")[0]; 
     var notificationId = treeView.dataSource.getByUid(li.getAttribute('data-uid')).ID; 

     if (notificationId == undefined) 
     { 
       //if the notification ID is "undefined" here, I want the ".EACH" 'loop' to continue to the next item. 
     } 
     $.ajax(
      { 
       url: '../api/notifications/deleteNotification?userId=' + userId + '&notificationId=' + notificationId, 
       type: 'DELETE' 
      }).done(function() 
      { 
       var treeview = $("#treeview").data("kendoTreeView"); 
       treeview.destroy(); 
       CreateNotificationTree(userId); 
       console.log('Delete successful.'); 
      }).fail(function(jqXHR, status, error) 
      { 
       console.log("Error : " + error); 
      }); 
     treeView.remove($(this).closest('.k-item')); 

    }); 
}); 

回答

7

您正在尋找return

if (typeof notificationId == "undefined") 
{ 
    // exit the current function 
    return; 
} 

這有去到下一個迭代的效果,因爲each只是調用匿名函數爲每個迭代。

+1

請注意,您可以返回除「false」之外的任何內容以繼續執行每個循環。 – David

+0

謝謝,這個伎倆。我可以在3分鐘內接受你的答案。 –

0
return false; 

很簡單,工作方式相同break在傳統的循環

編輯:對不起,你需要繼續下去,不會打破。抱歉。

+2

'break≠continue' –

+0

啊,你說得對。我誤解了。 –