JavaScript运行时错误:预期的函数

时间:2015-09-21 17:28:50

标签: javascript jquery syntax kendo-ui

我有以下准备功能。

$(document).ready(function() {
    var window = $("#window");
    viewModel.validator = $("#Details").kendoValidator().data("kendoValidator")({
        rules: {
            checkPersonContactInfo: function (input) {
                if (intakeView.viewModel.get("personDetails.Email") == '' && intakeView.viewModel.get("personDetails.CellPhone") == '' && intakeView.viewModel.get("personDetails.HomePhone") == '' && intakeView.viewModel.get("personDetails.WorkPhone") == '') {
                    return false;
                }
                else {
                    return true;
                }
            }

        },
    });
    if (!window.data("kendoWindow")) {
        window.kendoWindow({
            width: "600px",
            title: "Valdiation errors",
            visible: false
        });
    };
});

每当我调试它时,就会发出错误并且错误地说#34; 0x800a138a - JavaScript运行时错误:函数预期"。我多次检查是否缺少任何括号但我看不到任何东西。

4 个答案:

答案 0 :(得分:0)

编辑:发现错误的语法,你有一个不需要的逗号&分号:

$(document).ready(function() {
    var window = $("#window");
    viewModel.validator = $("#Details").kendoValidator().data("kendoValidator")({
        rules: {
            checkPersonContactInfo: function (input) {
                if (intakeView.viewModel.get("personDetails.Email") == '' && intakeView.viewModel.get("personDetails.CellPhone") == '' && intakeView.viewModel.get("personDetails.HomePhone") == '' && intakeView.viewModel.get("personDetails.WorkPhone") == '') {
                    return false;
                }
                else {
                    return true;
                }
            }

        } // here there was a comma which was not needed.
    });
    if (!window.data("kendoWindow")) {
        window.kendoWindow({
            width: "600px",
            title: "Valdiation errors",
            visible: false
        });
    } //here you had also a trailing ; which I removed
});

答案 1 :(得分:0)

尾随逗号可能是问题:

 }, 

答案 2 :(得分:0)

之前提到的尾随逗号是您的代码中的问题。 JavaScript对象文字中的尾随逗号自ECMAScript 5规范起有效,因此在最现代的浏览器中有效。然而,IE存在问题,在较旧的浏览器中会出现语法错误。

我只是在WebStorm的帮助下优化了它,这是WebStorm的输出:-)(你的代码中还有一个不必要的分号):

$(document).ready(function() {
    var window = $("#window");
    var viewModel = {};
    viewModel.validator = $("#Details").kendoValidator().data("kendoValidator")({
        rules: {
            checkPersonContactInfo: function (input) {
                return !(intakeView.viewModel.get("personDetails.Email") == '' && intakeView.viewModel.get("personDetails.CellPhone") == '' && intakeView.viewModel.get("personDetails.HomePhone") == '' && intakeView.viewModel.get("personDetails.WorkPhone") == '');
            }
        }
    });
    if (!window.data("kendoWindow")) {
        window.kendoWindow({
            width: "600px",
            title: "Valdiation errors",
            visible: false
        });
    }
});

答案 3 :(得分:0)

我认为你正在尝试这样做:

viewModel.validator = $("#Details").kendoValidator({//this is the change you need
        rules: {
            checkPersonContactInfo: function (input) {
                if (intakeView.viewModel.get("personDetails.Email") == '' && intakeView.viewModel.get("personDetails.CellPhone") == '' && intakeView.viewModel.get("personDetails.HomePhone") == '' && intakeView.viewModel.get("personDetails.WorkPhone") == '') {
                    return false;
                }
                else {
                    return true;
                }
            }

        },
    });

更多信息: see this