说分配给变量时我的函数是未定义的

时间:2012-09-17 17:56:05

标签: javascript asp.net html css

我有一个问题。我试图将函数返回值添加到变量,但它表示函数未定义。这是我的代码。 :

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;
        }

1 个答案:

答案 0 :(得分:2)

我注意到您的功能代码的标签比调用它的代码多一次。您的函数可能是不正确的范围(例如,它在另一个函数中声明,并且您从该函数外部调用它)。

如果您的功能未定义,那几乎肯定是一个范围问题。我觉得这个功能本身并没有错。

例如:

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

var bob = myFunction(); //Error: myFunction is not defined.