2

我有个问题。我正在尝试将函数返回值添加到变量中,但它说该函数未定义。这是我的代码。:

var selectedExpenseList = getSelectedExpenseIDs();

这是我的功能:

    function getSelectedExpenseIDs() {
            var selectedExpensesList = new Array;
            var i = 0;
            $('.expenseCheckBox:checked').each(function () {
                if ($(this)[0].id !== "checkAllExpenses") {
                    selectedExpensesList[i] = $(this)[0].id.split('_')[1];
                    ++i;
                }
            });
            return selectedExpensesList;
        }

编辑:这是我的全部功能:如果有人检查过,我会尝试从列表中删除某些内容。

     var selectedExpenseList;
 function actuallyDeleteTheExpense(canDeleteExpenses) 
 {
     selectedTasksList = getSelectedTaskIDs();
     var deleteTrackers = false, deleteExpenses = false;


     if (canDeleteExpenses && !canDeleteTrackers) 
     {
         $.Zebra_Dialog('Do you wish to remove Expenses?', 
         {
             'type': 'question',
             'title': 'Confirmation',
             'buttons': [
                        {
                            caption: 'Yes', callback: function () {
                                deleteTrackers = false;
                                deleteExpenses = true;
                                doTheDelete(deleteExpenses);
                            }
                        },

                        {
                            caption: 'No',
                            callback: function () {
                                doTheDelete(deleteExpenses);
                            }
                        }
                    ]
         });
     }
            }
            function doTheDelete(doIDeleteExpenses) 
            {
                if (selectedTasksList.length > 0) 
                {
                    $.ajax({
                        type: "POST",
                        //url: "/Tasks/ViewTasks.aspx/deleteTasksAndLinkedItems",
                        url: '<%=ResolveUrl("~/Expenses/ViewExpenses.aspx/deleteSelectedExpense")%>',
                        data: "{ 'TaskIDs': [" + selectedTasksList.join(',') + "], DeleteTrackers : " + doIDeleteTrackers + ", DeleteExpenses : " + doIDeleteExpenses + " }",
                        //fix data
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (data) {
                            var ss = data.d;
                            if (ss.length > 0) {
                                for (var i = 0; i < ss.length; ++i) {
                                    $.noty.consumeAlert({ layout: 'center', type: 'error', dismissQueue: true });
                                    alert(ss[i]);

                                }
                            }
                            $("#viewTasksGrid").flexReload();
                        },
                        error: function (data) {
                            $.noty.consumeAlert({ layout: 'center', type: 'error', dismissQueue: true, modal: true });
                            alert('Error Deleting Tasks');
                            if (window.console) {
                                console.log(data);
                            }
                        }
                    });
                } else {
                    showMessage('No tasks are selected.');
                }
            }
//end delete expense


        function getSelectedExpenseIDs() {
            var selectedExpensesList = new Array;
            var i = 0;
            $('.expenseCheckBox:checked').each(function () {
                if ($(this)[0].id !== "checkAllExpenses") {
                    selectedExpensesList[i] = $(this)[0].id.split('_')[1];
                    ++i;
                }
            });
            return selectedExpensesList;
        }
4

1 回答 1

2

我注意到你的函数代码比你调用它的代码多了一个选项卡。您的函数的范围可能不正确(例如,它在另一个函数内声明,而您从该函数外部调用它)。

如果您的函数以未定义的形式返回,则几乎可以肯定这是一个范围问题。我认为函数本身没有任何问题。

例如:

$(document).ready(function () {
    function myFunction() { 
        return 3;
    } 
});

var bob = myFunction(); //Error: myFunction is not defined.
于 2012-09-17T18:03:13.253 回答